Docsity

Switch-Computer Fundamentals-Lecture Slides, Slides of Computer Fundamentals

Toggle sidebar
bg1
.
19
4.7 The switch Multiple-Selection Statement
• switch
– Useful when a variable or expression is tested for all the
values it can assume and different actions are taken
• Format
– Series of case labels and an optional default case
switch ( value ){
case '1':
actions
case '2':
actions
default:
actions
}
– break; exits from statement
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Switch-Computer Fundamentals-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

4.7 The switch Multiple-Selection Statement

• switch

– Useful when a variable or expression is tested for all the

values it can assume and different actions are taken

• Format

– Series of case labels and an optional default case

switch ( value ){

case '1':

actions

case '2':

actions

default:

actions

– break; exits from statement

4.7 The switch Multiple-Selection Statement

• Flowchart of the switch statement

true

false

case a case a action(s) break

case b case b action(s) break

false

false

case z case z action(s) break

true

true

default action(s)

OutlineOutline

fig04_07.c (Part 2 of

OutlineOutline

fig04_07.c (Part 3 of

4.8 The do … while Repetition Statement

• The do…while repetition statement

– Similar to the while structure

– Condition for repetition tested after the body of the loop is

performed

• All actions are performed at least once

– Format:

do {

statement ;

} while ( condition );

4.8 The do … while Repetition Statement

• Example (letting counter = 1):

do {

printf( "%d ", counter );

} while (++counter <= 10);

– Prints the integers from 1 to 10

OutlineOutline

fig04_09.c

Program Output

4.9 The break and continue Statements

• break

– Causes immediate exit from a while, for, do…while or

switch statement

– Program execution continues with the first statement after

the structure

– Common uses of the break statement

• Escape early from a loop

• Skip the remainder of a switch statement

4.9 The break and continue Statements

• continue

– Skips the remaining statements in the body of a while, for

or do…while statement

• Proceeds with the next iteration of the loop

– while and do…while

• Loop-continuation test is evaluated immediately after the

continue statement is executed

– for

• Increment expression is executed, then the loop-continuation

test is evaluated

OutlineOutline

fig04_12.c

Program Output

Used continue to skip printing the value 5