Information Technology Reference
In-Depth Information
The if Statement
The if statement implements conditional execution. The syntax for the if statement is shown
here, and is illustrated in Figure 9-1.
￿f TestExpr evaluates to true , Statement is executed.
￿f t evaluates to false , Statement is skipped.
￿ TestExpr must evaluate to a value of type bool .
if( TestExpr )
Statement
Figure 9-1. The if statement
The following code shows examples of if statements:
// With a simple statement
if( x <= 10 )
z = x - 1; // Single statement, no curly braces needed
// With a block
if( x >= 20 )
{
x = x - 5; // Block--braces needed
y = x + z;
}
int x = 5;
if( x ) // Error: test expression must be a bool, not int
{
...
}
Search WWH ::




Custom Search