If and If-Else Statements

The most basic manner to control the flow of a program is to use if-statements. An if-statement combines a block of code, called the statement body, with a condition to be evaluated before executing the statement body. The condition guards the body. If the condition is true, the body is executed, else it is skipped. For instance,
   if ( i!= 0 ) 
   {
      cout << j%i ;
   }
   else
   {
      cout << "Ugly" ;
   }
The modulus will not be preformed if i is zero. Which is good, because that calculation would cause the program to break. Here we have demonstrated the optional use of an else clause. Either the if part or the else part is executed. We use the else part to complain about the narrowly missed disaster.

Review the Syntax of Expressions if the following notation is unfamiliar to you. Here is the syntax of the statements we are currently studying:

A statement is any of the following:

A compound-statement is of the form: and a statement-list is of the form:

The compound-statement is a very important construct. You have already used one compound statement - the body of main must be of that form. An if-statement which uses a compound statement for its body demonstrates that compound-statements can nest inside of compound-statements. This is what the syntax says: a compound-statement is a series of statements, and a statement can be a compound-statement. This is legal, although strange:

    { 
        i = 1 ;
        {
           j = 2 ;
        }
    }
Note: In C++ you must declare all variables before use. The above example is a good snippet of syntax, but it is not complete.

The abouve example derives from the syntax:

statement
=>compound-statment
=> { statement-list }
=> { statement statement-list }
=> { expression-statement statement }
=> { expression ; compound-statement }
=> { i = 1 ; { statement-list } }
=> { i = 1 ; { statement } }
=> { i = 1 ; { expression-statment } }
=> { i = 1 ; { expression ; } }
=> { i = 1 ; { j = 2 ; } }

According to the compiler it is optional how you indent your code. According to me, it is manditory to indent your code according to the pattern I've demonstrated. Make your compound-statements easy to see by placing the opening and closing curly braces on their own lines, and lined up vertically. Indent by an additional 3 spaces each statement which is enclosed by the curly braces.

It is easiest if you only use the compound-statement form of the if- and if-else-statements. Please begin your C++ experience by using only that format.

The use of compound statements which are either run entirely or skipped over entirely, while, for the most part, the flow of the code runs from top of the page to the bottom of the page, is a major tenant of so-called structured programming. Before structured programming, programmers were very careless about how control snaked around the page, mostly seeking to minimize the amount of code written. But it turned out that making bug-free programs is very hard, and people began to look for any technique that would give them an advantage in creating bug-free code.

Having code flow simply from top to bottom, with the exception of a few powerful control constructs, such as the complete execution of or the complete omission of an if-body according to the outcome of a condition, made it possible to keep track of what had been done, and what was left to be done, at any particular point in the program's text. These are called assertions. More will be said about assertions later in the term.

We now explain the details of the evaluation of the if-condition. In the form,

if ( expression ) compound-statement first the expression is evaluated. If it evaluates non-zero, then the compound-statement is executed, else it is skipped. In the form, if ( expression ) compound-statement else compound-statment first the expression is evaluated, If it evaluates non-zero, the first compound-statement is executed, but not the second, else the second is executed, but not the first.

Note well that the expression is to be an integer. In the case:

    if ( i!=0 ) 
    {
       cout << j%i ;
    }
the expression i!=0 reads i not equal to zero, and seems to result in true or false, actually, the value of a relational-operator in an expression is 0 if false, 1 if true. Hence, here is another way to write this:
   if ( i )
   {
      cout << j%i ;
   }
If i happened to be zero, it would be considered false, and the compound-statement would be skipped. The value of this convention will be clear later on.

Burton Rosenberg. Last Modified January 25, 1997, 11:09 PM