A sample execution of the program results the following output.

 

Now think what happens if the condition in the if statement is not true i.e. Amer’s age is not greater than Amara’s. In this case, if the user enters Amer’s age less than Amara’s, then our program does nothing. So to check this condition, another if statement after the first if statement is required. Then our program will be as:

.

# include <iostream.h>

 

main ( )

{

            int AmerAge, AmaraAge;

            //prompt the user to enter Amer’s age

            cout << “Please enter Amer’s age     “ ;

            cin >> AmerAge;

            //prompt the user to enter Amara’s age

            cout << “Please enter Amara’s age     “ ;

            cin >> AmaraAge;

            //perform the test

            if (AmerAge > AmaraAge )

            { 

                  cout << “ Amer is older than Amara”;

            }

            if (AmerAge < AmaraAge )

            {

         cout << “ Amer is younger than Amara”;

            }

}

 

 

Now our program decides properly about the ages entered by the user.

After getting ages from the user, the if statements are tested and if statement will be executed if the condition evaluates to true.

 

 

 

Previous

 

 

TOC

 

 

Next