The if keyword is used to execute a statement or block of statements when it's associated expression evaluates to true. An if statement may also have an alternative else clause. If the expression defined by the if statement evaluates to false, then control transfers to the statement (or block of statements) following the else keyword. If the expression defined by the if statement evaluates to true, then control transfers to the first statement following the if keyword.
Note that if/else conditional testing is a frequent source of program errors. Consider the following example of a poorly formatted if/else condition:
if ( numPrints < 1000 )
if ( paperFeed == fullTray )
{
printPaperCopy( );
showStatus( );
paperFeed--;
}
else
suspendPrintJob( ); // Bad indenting!
In the example shown above, the beginning of the else statement block is formatted to fall into the same column as the outermost if statement, even though it's proper logical association is with the if statement closest to it. Problems of this nature can be easily overlooked, unfortunately and it is up to the programmer to use good formatting techniques to illustrate the logical intent of the code.