Information Technology Reference
In-Depth Information
The if...else Statement
The
if...else
statement implements a two-way branch. The syntax for the
if...else
state-
ment is shown here, and is illustrated in Figure 9-2.
f
TestExpr
evaluates to
true
,
Statement1
is executed.
f t evaluates to
false
,
Statement2
is executed instead.
if(
TestExpr
)
Statement1
else
Statement2
Figure 9-2.
The if...else statement
The following is an example of the
if...else
statement:
if( x <= 10 )
z = x - 1; // Single statement--no braces needed
else
{ // Multiple statements--block
x = x - 5;
y = x + z;
}