Beruflich Dokumente
Kultur Dokumente
el se i f ( expression )
statement
el se
statement
Copyright 2008 W. W. Norton & Company.
All rights reserved.
27
Chapter 5: Selection Statements
Program: Calculating a Brokers Commission
When stocks are sold or purchased through a broker, the
brokers commission often depends upon the value of the
stocks traded.
Suppose that a broker charges the amounts shown in the
following table:
Transaction size Commission rate
Under $2,500 $30 +1.7%
$2,500$6,250 $56 +0.66%
$6,250$20,000 $76 +0.34%
$20,000$50,000 $100 +0.22%
$50,000$500,000 $155 +0.11%
Over $500,000 $255 +0.09%
The minimum charge is $39.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
28
Chapter 5: Selection Statements
Program: Calculating a Brokers Commission
The br oker . c program asks the user to enter the
amount of the trade, then displays the amount of
the commission:
Ent er val ue of t r ade: 30000
Commi ssi on: $166. 00
The heart of the program is a cascaded i f
statement that determines which range the trade
falls into.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
29
Chapter 5: Selection Statements
broker.c
/ * Cal cul at es a br oker ' s commi ssi on */
#i ncl ude <st di o. h>
i nt mai n( voi d)
{
f l oat commi ssi on, val ue;
pr i nt f ( " Ent er val ue of t r ade: " ) ;
scanf ( " %f " , &val ue) ;
i f ( val ue < 2500. 00f )
commi ssi on = 30. 00f + . 017f * val ue;
el se i f ( val ue < 6250. 00f )
commi ssi on = 56. 00f + . 0066f * val ue;
el se i f ( val ue < 20000. 00f )
commi ssi on = 76. 00f + . 0034f * val ue;
el se i f ( val ue < 50000. 00f )
commi ssi on = 100. 00f + . 0022f * val ue;
el se i f ( val ue < 500000. 00f )
commi ssi on = 155. 00f + . 0011f * val ue;
el se
commi ssi on = 255. 00f + . 0009f * val ue;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
30
Chapter 5: Selection Statements
i f ( commi ssi on < 39. 00f )
commi ssi on = 39. 00f ;
pr i nt f ( " Commi ssi on: $%. 2f \ n" , commi ssi on) ;
r et ur n 0;
}
Copyright 2008 W. W. Norton & Company.
All rights reserved.
31
Chapter 5: Selection Statements
The Dangling else Problem
When if statements are nested, the dangling el se
problem may occur:
i f ( y ! = 0)
i f ( x ! = 0)
r esul t = x / y;
el se
pr i nt f ( " Er r or : y i s equal t o 0\ n" ) ;
The indentation suggests that the el se clause belongs
to the outer i f statement.
However, C follows the rule that an el se clause
belongs to the nearest i f statement that hasnt already
been paired with an el se.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
32
Chapter 5: Selection Statements
The Dangling else Problem
A correctly indented version would look like this:
i f ( y ! = 0)
i f ( x ! = 0)
r esul t = x / y;
el se
pr i nt f ( " Er r or : y i s equal t o 0\ n" ) ;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
33
Chapter 5: Selection Statements
The Dangling else Problem
To make the el se clause part of the outer i f
statement, we can enclose the inner i f statement
in braces:
i f ( y ! = 0) {
i f ( x ! = 0)
r esul t = x / y;
} el se
pr i nt f ( " Er r or : y i s equal t o 0\ n" ) ;
Using braces in the original i f statement would
have avoided the problem in the first place.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
34
Chapter 5: Selection Statements
Conditional Expressions
Cs conditional operator allows an expression to
produce one of two values depending on the value
of a condition.
The conditional operator consists of two symbols
(? and : ), which must be used together:
expr1 ? expr2 : expr3
The operands can be of any type.
The resulting expression is said to be a
conditional expression.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
35
Chapter 5: Selection Statements
Conditional Expressions
The conditional operator requires three operands,
so it is often referred to as a ternary operator.
The conditional expression expr1 ? expr2 : expr3
should be read if expr1 then expr2 else expr3.
The expression is evaluated in stages: expr1 is
evaluated first; if its value isnt zero, then expr2 is
evaluated, and its value is the value of the entire
conditional expression.
If the value of expr1 is zero, then the value of
expr3 is the value of the conditional.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
36
Chapter 5: Selection Statements
Conditional Expressions
Example:
i nt i , j , k;
i = 1;
j = 2;
k = i > j ? i : j ; / * k i s now 2 */
k = ( i >= 0 ? i : 0) + j ; / * k i s now 3 */
The parentheses are necessary, because the
precedence of the conditional operator is less than
that of the other operators discussed so far, with
the exception of the assignment operators.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
37
Chapter 5: Selection Statements
Conditional Expressions
Conditional expressions tend to make programs
shorter but harder to understand, so its probably
best to use them sparingly.
Conditional expressions are often used in r et ur n
statements:
r et ur n i > j ? i : j ;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
38
Chapter 5: Selection Statements
Conditional Expressions
Calls of pr i nt f can sometimes benefit from
condition expressions. Instead of
i f ( i > j )
pr i nt f ( " %d\ n" , i ) ;
el se
pr i nt f ( " %d\ n" , j ) ;
we could simply write
pr i nt f ( " %d\ n" , i > j ? i : j ) ;
Conditional expressions are also common in
certain kinds of macro definitions.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
39
Chapter 5: Selection Statements
Boolean Values in C89
For many years, the C language lacked a proper
Boolean type, and there is none defined in the C89
standard.
One way to work around this limitation is to declare
an i nt variable and then assign it either 0 or 1:
i nt f l ag;
f l ag = 0;
f l ag = 1;
Although this scheme works, it doesnt contribute
much to program readability.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
40
Chapter 5: Selection Statements
Boolean Values in C89
To make programs more understandable, C89
programmers often define macros with names
such as TRUE and FALSE:
#def i ne TRUE 1
#def i ne FALSE 0
Assignments to f l ag now have a more natural
appearance:
f l ag = FALSE;
f l ag = TRUE;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
41
Chapter 5: Selection Statements
Boolean Values in C89
To test whether f l ag is true, we can write
i f ( f l ag == TRUE)
or just
i f ( f l ag)
The latter form is more concise. It also works
correctly if f l ag has a value other than 0 or 1.
To test whether f l ag is false, we can write
i f ( f l ag == FALSE)
or
i f ( ! f l ag)
Copyright 2008 W. W. Norton & Company.
All rights reserved.
42
Chapter 5: Selection Statements
Boolean Values in C89
Carrying this idea one step further, we might even
define a macro that can be used as a type:
#def i ne BOOL i nt
BOOL can take the place of i nt when declaring
Boolean variables:
BOOL f l ag;
Its now clear that f l ag isnt an ordinary integer
variable, but instead represents a Boolean
condition.
Copyright 2008 W. W. Norton & Company.
All rights reserved.
43
Chapter 5: Selection Statements
Boolean Values in C99
C99 provides the _Bool type.
A Boolean variable can be declared by writing
_Bool f l ag;
_Bool is an integer type, so a _Bool variable is
really just an integer variable in disguise.
Unlike an ordinary integer variable, however, a
_Bool variable can only be assigned 0 or 1.
Attempting to store a nonzero value into a _Bool
variable will cause the variable to be assigned 1:
f l ag = 5; / * f l ag i s assi gned 1 */
Copyright 2008 W. W. Norton & Company.
All rights reserved.
44
Chapter 5: Selection Statements
Boolean Values in C99
Its legal (although not advisable) to perform
arithmetic on _Bool variables.
Its also legal to print a _Bool variable (either 0
or 1 will be displayed).
And, of course, a _Bool variable can be tested in
an i f statement:
i f ( f l ag) / * t est s whet her f l ag i s 1 */
f l ag = t r ue;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
46
Chapter 5: Selection Statements
The switch Statement
A cascaded i f statement can be used to compare an
expression against a series of values:
i f ( gr ade == 4)
pr i nt f ( " Excel l ent " ) ;
el se i f ( gr ade == 3)
pr i nt f ( " Good" ) ;
el se i f ( gr ade == 2)
pr i nt f ( " Aver age" ) ;
el se i f ( gr ade == 1)
pr i nt f ( " Poor " ) ;
el se i f ( gr ade == 0)
pr i nt f ( " Fai l i ng" ) ;
el se
pr i nt f ( " I l l egal gr ade" ) ;
Copyright 2008 W. W. Norton & Company.
All rights reserved.
47
Chapter 5: Selection Statements
The switch Statement
The swi t ch statement is an alternative:
swi t ch ( gr ade) {
case 4: pr i nt f ( " Excel l ent " ) ;
br eak;
case 3: pr i nt f ( " Good" ) ;
br eak;
case 2: pr i nt f ( " Aver age" ) ;
br eak;
case 1: pr i nt f ( " Poor " ) ;
br eak;
case 0: pr i nt f ( " Fai l i ng" ) ;
br eak;
def aul t : pr i nt f ( " I l l egal gr ade" ) ;
br eak;
}
Copyright 2008 W. W. Norton & Company.
All rights reserved.
48
Chapter 5: Selection Statements
The switch Statement
A swi t ch statement may be easier to read than a
cascaded i f statement.
swi t ch statements are often faster than i f
statements.
Most common form of the swi t ch statement:
swi t ch ( expression ) {
case constant-expression : statements