Sams Teach Yourself C in 21 Days

(singke) #1
statement1;
}
else
{
statement2;
}
next_statement;
This is the most common form of the ifstatement. If expressionis true,statement1is
executed; otherwise,statement2is executed.
Form 3
if( expression1)
statement1;
else if( expression2)
statement2;
else
statement3;
next_statement;
This is a nested if. If the first expression,expression1, is true,statement1is executed
before the program continues with the next_statement. If the first expression is not true,
the second expression,expression2, is checked. If the first expression is not true, and
the second is true,statement2is executed. If both expressions are false,statement3is
executed. Only one of the three statements is executed.
Example 1
if( salary > 45,0000 )
{
tax = .30;
}
else
{
tax = .25;
}
Example 2
if( age < 18 )
printf(“Minor”);
else if( age < 65 )
printf(“Adult”);
else
printf( “Senior Citizen”);

Evaluating Relational Expressions ......................................................................


Remember that expressions using relational operators are true C expressions that evalu-
ate, by definition, to a value. Relational expressions evaluate to a value of either false ( 0 )

78 Day 4

,


,


07 448201x-CH04 8/13/02 11:15 AM Page 78

Free download pdf