Unit#2 Control Structures - Selection (If statements)
Course Outline Return to Unit
If statements


Using these statements we can skip over a block of  instructions in our program. Consider the following diagram:

#include<iostream.h>
#include<math.h>


main( )
{
Declarations of variables
Block #1 of instructions
if (Condition )
{
Block #2 of instructions
}
Block #4
}


Blocks #1 and #3 will be executed in all cases but block #2 will be executed only if the condition of the if statement is true, otherwise it will be skipped.

Note: when block #2 consists of a single statement, then the brackets are not required.  The statement may be written next to if ( condition ).

 
Example
2ex2.appended

 

Jacob Y. Kazakia © 2001 All rights reserved