If/else Structure

 

 

We have seen that the if structure executes its block of statement(s) only when the condition is true, otherwise the statements are skipped. The if/else structure allows the programmer to specify that a different block of statement(s) is to be executed when the condition is false. The structure of if/else selection is as follows.

 

if ( condition)

{

statement(s);

}

else

{

statement(s);

}

 

Thus using this structure we can write the construct of our program as

 

if (AmerAge > AmaraAge )

{

cout << " Amer is older than Amara";

}

else

{

cout << " Amer is younger than Amara";

}

 

In this construct, the program checks the condition in if statement .If the condition is true, then the line "Amer is greater than Amara" is printed. Otherwise (if condition is not true), the statement related to else is executed and the message "Amer is younger than Amara" is printed. Here in if/else structure an important thing is that the else part is executed for all the cases (conditions) other than the case which is stated in the if condition.

 

 

 

Previous

 

 

TOC

 

 

Next