if Statement

Scheme (if expr S_1 ) (if expr S_1 S_2 )
Pascal if expr then S_1 if expr then S_1 else S_2
C++, Java if ( expr ) S_1 if ( expr ) S_1 else S_2
expr ? expr_1 : expr_2

The else part of the statement is optional. If multiple statements are to be included in the then or else part, a begin - end or { } must be used.

The operator pair ? : may be used in C++ and Java for expressions only.

Scheme: (set! larger (if (>= x y) x y))
C++, Java: larger = (x >= y) ? x : y

There is a language ambiguity regarding the else part of nested if statements:


   if amount > balance
      then if student then bounce
                      else pay
Rule: The else belongs to the closest if statement.

Contents    Page-10    Prev    Next    Page+10    Index