The if-else-Statement
-
The syntax of the if-else-statement is:
if (Boolean_Expression)
Yes_statement
else
No_statement
The Boolean expression is first evaluated to see if it is true or false. If it is true then the Yes_statement is executed and then the No-statement is skipped, otherwise the Yes_statement is skipped and the No-statement is executed.
In either case control is passed on to the statement that follows the if-else--block. Again compound statements can be used instead of single Yes_statement or No_statement.
-
The else part of an if-else statement is actually optional, which when left out simply gives the if statement.
-
For example, the following code:
float LargeBonus = 230.00, SmallBonus = 130.00;
if (number_of_sales > target)
{
Salary += LargeBonus;
cout << "What a superb job you are doing!\n";
}
else
{
Salary += SmallBonus;
cout << "What a fine job you are doing!\n";
}
cout << "Salary = " << Salary << endl;
adds a large bonus money of 230 dollars to the salary and prints out a very nice comment if the number of sales exceed the target number, otherwise a small bonus money of 130 is added and a nice comment is printed out.
In any case the actual salary is always printed out.