The if-else-Statement

  1. 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 skips over the No-statement, otherwise it skips over the yes_statement and execute the No-statement. In either case control is passed on to the statement that follows the if-else--block. Again compound statements can be used.

  2. 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.