Solution for Exercise 8-10

ex0810

#include <stdio.h>

int main()
{
    int a,b;

    a = 6;
    b = a - 2;

    if( a > b)
    {
        printf("%d is greater than %d\n",a,b);
    }
    else
    {
        printf("%d is not greater than %d\n",a,b);
    }
    return(0);
}

Output

6 is greater than 4

Notes

* Line 16 doesn't print "is less than" because it's not the logical conclusion. The value can be less than, but also could be equal to. The phrase "is not greater than" is best.

* You need now use curly brackets when only one statement follows if. This rule also holds for the else part of the structure: When only one statement follows else, you can dispense with the curly brackets.

* By the way, each part of the if-else structure is independent. So you can have multiple statements belonging to if — in which case you need the curly brackets — but when the else part has only one statement, you don't need to block the statement in curly brackets.