Vous êtes sur la page 1sur 17

Multiway Selection

In addition to two-way selection, most programming languages provide another selection


concept known as multiway selection. Multiway selection chooses among several alternatives.
The decision logic for the multiway statement is seen in figure 5-19.

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

Value Value Value


Value
1 2 3
4acti
actio actio actio
on
n n n

Figure 5-19 switch Decision Logic

The switch Statement


Switch is a composite statement used to make a decision between many alternatives. Figure 5-20
shows the switch statement syntax.

switch (expression)

case constant-1: statement


.

statement

case constant-2: statement

statement

case constant-n: statement

Statement

default: statement

Statement

} // end switch

Figure 5-20 switch Statement Syntax


printF
lag

This This This


is is is
cas cas defa
e1 e2 ult

(a) Logic Flow

switch (printFlag)

case 1:

Printf

(“This is case 1”);

break;

case 2:

Printf

(“This is case 2”);

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’

Figure 5-24 The else-if Logic Design for program


An action or
Condit series of
ion actions

An action or
Condit
series of
ion
actions

(a) Pretest Loop (b) post-test Loop

Figure 6-2 Pretest and Post-test Loops

Ener
gy
Do
one
push-
Ener
? gy
Do
one
push-
?

(a) Pretest Loop (b) post-test Loop

Figure 6-3 Two Different Strategies for Doing Loop

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

Figure 6-4 Minimum Number of Iterations in Two Loops

Initializati
Initializati
Te
Action(
st
Updatin

Action( Te
st
Updatin

(a) Pretest Loop (b) Post-test Loop

Figure 6-5 Loop Initialization and Updating

Creat
Creat
e
e
Energ
Energ

Ene Do
gry One
OK? Push-
Ene
Do gry
One OK?
Energy
Reduce
dd

Energy
Reduce
dd

(a) Pretest Loop (b) post-


test Loop

Figure 6-6 Initialization and Updating for Exercise

Initialize Initialize
Conditio Conditio

false
Condit
ion Action(
s)

Update
Conditio
n
Action(
s)
Update Condit
Conditio ion
n
true

exit

(a) Pretest Loop (b) Post-test


Loop

Figure 6-7 Event-controlled Loop Concept

Set Set
count count
to 0 to 0
count
< n Action(
s)
increme
nt count
Action(
s)
increme count
nt count < n

(a) Pretest Loop (b)


Post-test Loop

Figure 6-8 Counter-controlled Loop Concept


Loop
s

whil for do….whi


e le

Figure 6-9 C Loop Constructs

expres
sion

stateme
nt

(a) Flowchart

while (expression)

statement

(b) Sample Code

Figure 6-10 The while Statement

While (expression)
expres
sion
{

Actio Actio

. Actio
Actio
.

.
Actio
Actio n

} /
/while

(a) Flowchart (b) C Language

For (expr1; expr2;


expr3;) statement

Figure 6-12 for statement

For (expr1;

expr2;

expr3)

{
Action
Action
Action
. Action

. .
Action
.
Action

} //for

(a) Flowchart (b) C


Language
Figure 6-13 Compound for Statement

for (initializ
initializ e;
e expressio
n;
while (expressio Updat
n)
{

{ actio
n
actio actio
n n
actio } // for
n
updat

} //while

Figure 6-14 Comparing for and while Loops

Flowchart Sample Code


do
stateme
nt stateme
express nt
while (expressio ;
ion
n)

do

Actio {
n Actio
Actio n
n . Actio
n
. .
Actio .
n
Actio
express n
ion } While (expressio ;
n)

Figure 6-15 do…while Statement

Pretest
nothing
prints
While (false) do

{ {
Printf(“Hello World”); printf(“Hello World”);

} //while
} while (false);
prints
Hello…
Post-test

Figure 6-16 Pre- and Post-test Loops

The Comma Expression

A comma expression is a complex expression made up of two expressions


separated by a comma. Although it can legally be used in many places, it is most
often used in for statements. The expression are evaluated left to right. The value
and type of the expressions are the value and type of the right expression; the left
expression included only for its side effect. The comma expression has the lowest
priority of all expressions, priority 1.

The following statement is a modification of the for statement code shown


on page 317. It uses a comma expression to initialize the accumulator, sum, and the
index, I, in the loops. In this example, the value of the comma expression is
discarded. This is a common use of the comma operator.

For (sum=0; i=1; i<=20; i++)

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

// more while processing

….

} // while

Figure 6-20 break and Inner Loops


Program 6-19 sum to EOF Function

/*Read a series of numbers, terminated by EOF, and return their sun to the
calling program.

Pre nothing

Post data read and sum returned

*/

Int sumEOF (void)

//Local declarations

Int nmbr;

Int sum;

//statements

Sum = 0;

Printf(“please enter an integer: “);

While (scanf(“%d”, &nmbr) !=EOF)

Sum += nmbr;

Printf(“Next integer <EOF> to stop: “);

} //while

Return sum;

} // sumEOF
PROGRAM 6-20 Powers Function

/* Raise base to an integral power, exp. If the exponent is zero, retuen1.

Pre base & exp are both positive integer values

Post return either (a) the result of raising the base to the exp power.

Or (b) zero if the parameters are invalid

*/

Int powers (int base , int exp)

//Local Declarations

Int result = 1;

//statements

If (base < 1 || exp < 0)

// Error condition

Result = 0;

Else

For (int i=1;i<=exp;i++)

Result *=base;

Return result;

} //Powers

Program 6-21 Smallest to EOF Function

/*Read a series of numbers, terminated by EOF, and pass the smallest to


the calling program.

Pre nothing
Post data read and smallest returned

*/

Int smallestEOF(void)

//Local Declarations

Int numIn;

Int smallest;

//statements

Smallest = INT_MAX; //requires <limits.h>

Printf(“Please enter an integer: “);

While (scanf(“%d”, &numIn) !=EOF)

If (numIn < smallest)

Smallest = numIn;

printf(“enter Next integer <EOF> to stop: “);

} //while

return smallest;

} // smallestEOF

Vous aimerez peut-être aussi