Académique Documents
Professionnel Documents
Culture Documents
C has two different ways to implement multiway selection. The first is by switch
statement. The other is a programming technique known as the else-if that provides a convenient
style to nest if statements. The switch statement can be used only when the selection condition
reduces to an integral expression. Many times, however, such as when the selection is based on a
range of values, the condition is not an integral. In these cases, we use the else-if.
Multiw
ay
expres
sion
switch (expression)
statement
statement
Statement
default: statement
Statement
} // end switch
switch (printFlag)
case 1:
Printf
break;
case 2:
Printf
break;
default:
Printf
(“This is default”);
break;
}
(b) Code
sco
re
>=
sco 90
grade =
re
‘A’
>=
80
sco
re grade =
>= ‘B’
70
sco
re grade =
>= ‘C’
60
grade = grade =
‘F’ ‘D’
An action or
Condit
series of
ion
actions
Ener
gy
Do
one
push-
Ener
? gy
Do
one
push-
?
Test Body
1 1
In a pretest loop, the Test
body may not be 1
Body
1 In a post-test loop, the
body must be executed at
(a) Pretest (b) post-test
Initializati
Initializati
Te
Action(
st
Updatin
Action( Te
st
Updatin
Creat
Creat
e
e
Energ
Energ
Ene Do
gry One
OK? Push-
Ene
Do gry
One OK?
Energy
Reduce
dd
Energy
Reduce
dd
Initialize Initialize
Conditio Conditio
false
Condit
ion Action(
s)
Update
Conditio
n
Action(
s)
Update Condit
Conditio ion
n
true
exit
Set Set
count count
to 0 to 0
count
< n Action(
s)
increme
nt count
Action(
s)
increme count
nt count < n
expres
sion
stateme
nt
(a) Flowchart
while (expression)
statement
While (expression)
expres
sion
{
Actio Actio
. Actio
Actio
.
.
Actio
Actio n
} /
/while
For (expr1;
expr2;
expr3)
{
Action
Action
Action
. Action
. .
Action
.
Action
} //for
for (initializ
initializ e;
e expressio
n;
while (expressio Updat
n)
{
{ actio
n
actio actio
n n
actio } // for
n
updat
} //while
do
Actio {
n Actio
Actio n
n . Actio
n
. .
Actio .
n
Actio
express n
ion } While (expressio ;
n)
Pretest
nothing
prints
While (false) do
{ {
Printf(“Hello World”); printf(“Hello World”);
} //while
} while (false);
prints
Hello…
Post-test
Scanf(“%d”, $a);
Sum+=a;
} //for
Comma expressions can be nested. When they are nested, all expression values
other than the last are discarded. Figure 6-17 shows the format of a nested comma
expression.
While (condition)
…..
The break statement
For ( …; …; …) takes us out of the
{
inner loop (the for
…. loop). The while loop
is still active.
If (other condition)
break;
….
} //for
….
} // while
/*Read a series of numbers, terminated by EOF, and return their sun to the
calling program.
Pre nothing
*/
//Local declarations
Int nmbr;
Int sum;
//statements
Sum = 0;
Sum += nmbr;
} //while
Return sum;
} // sumEOF
PROGRAM 6-20 Powers Function
Post return either (a) the result of raising the base to the exp power.
*/
//Local Declarations
Int result = 1;
//statements
// Error condition
Result = 0;
Else
Result *=base;
Return result;
} //Powers
Pre nothing
Post data read and smallest returned
*/
Int smallestEOF(void)
//Local Declarations
Int numIn;
Int smallest;
//statements
Smallest = numIn;
} //while
return smallest;
} // smallestEOF