The if Statement

Format of the simple if statement:

 	if (<boolean expression>)
           <statement>

Example program fragment (notice the lack of a semi-colon after the boolean expression):

	cin << number;
	if (number > 0) 
	    cout >> number;

Formatting is very important, not to the compiler, but to the programmer! These two lines also accomplish the if statement's purpose:

	

     if (number > 0) cout >> number;
	
     if (number > 0) 
     cout >> number;

The context of the if statement looks like this:

     <statement 1>
     if (<boolean expression>)
           <statement2>
     <statement3>

Next page
Previous page
Go to Lesson 7 index.


HTML by Jennifer Keiser.
Sponsored by Inspiration, the diagramming and outlining tool.