Decision-making:

By: aathishankaran Printer Friendly Format    

Decision-making: The if construct

 

If a certain condition is true, you direct the program to take one course of action. If the condition is false, you direct the program to do something else. This is one of the most fundamental concepts in computer programming. This decision-making can be done by means of the if construct. The if construct makes use of relational and logical operators for decision-making. The simplest form of if construct is as follows:

 

if (condition)

{

            statement 1;

            statement 2;

}

 

Example:

 

To accept two numbers and verify and whether they are equal or not, the following pseudocode can be witten:

 

PROGRAM_EQUAL

{

            integer Number1;

            integer Number2;

            accept Number1;

            accept Number2;

            if(Number1 == Number2)

            {

                        display “Input numbers are equal”;

            }

}

 

If the numbers are equal, it displays the message “Input numbers are equal.”. If the are not equal, the execution of program sequence terminates without any message. To handle the situation when two numbers are not equal, the following pseudocode can be written as:

 

PROGRAM_EQUAL

{

            integer Number1, Number2;

            accept Number1;

            accept Number2;

            if(Number1 == Number2)

            {

                        display “Input numbers are equal.”;

            }

            else

            {

                        display ”Input numbers are not equal.”;

            }

}

 

The general form of if construct is as follows:

 

If( condition )

{

            statement1;

            statement2;

}

else

{

            statement3;

            statement4;

}

 





Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial