Title: Additional Control Structures
1Additional Control Structures
2New and Improved . . .
3Branching Statements
- Recall the current branching capability provided
by the if ( ) statement - Only branches twoways
- What if we desiremultiway branching?
4Multiway Branching
- C provides the switch statement
switch (choice) case 1 do_option_one()
breakcase 2 case 3 do_2_3_a ()
do_2_3_b () breakdefault do_something_else
()
5Switch Statement
- Value of the switch expression matched with one
of the labels attached to a branch - The statement(s) with the match get executed
switch (choice) case 1 do_option_one()
breakcase 2 case 3 do_2_3_a ()
do_2_3_b () breakdefault do_something_else
()
6Switch Statement
- Switch expression gt the expression in
parentheses whose value determines which switch
label is selected - cannot be floating point
- usually is int or char
- Identifiers following case must be constants
switch (choice) case 1 do_option_one()
breakcase 2 case 3 do_2_3_a ()
do_2_3_b () breakdefault do_something_else
()
7Switch Statement
- The break causes control to be shifted to first
statement after the switch statement - the default statement is executed if the value of
the switch expression is NOT found among switch
labels
switch (choice) case 1 do_option_one()
breakcase 2 case 3 do_2_3_a ()
do_2_3_b () breakdefault
do_something_else () // next statement
8A Different Looping Statement
- Recall that the while ( ) statement always
checked the condition BEFORE the loop - In certain situations wewish to check the
condition at the END ofthe loop
9The Do-While Statement
- Condition tested at end/bottom of loop
- Guarantees loop body executes at least once
10The do-while Illustrated
Note alwaysat least oneiteration
Loop Iteration
Loop Entry
Loop Exit
Loop Trest
11The Do-While Statement
- Can be used for a counting loop
What gets printed??
How do you change it to go 10 times?
12The Do While Statement
- This logic sometimes more suitable for some
algorithms - Example trap for valid input
do cout ltlt Enter value (1 - 5) -gt cin
gtgt value if (value lt 1 value gt 5)
cout ltlt Invalid input\a ltlt endl while
(value lt 1 value gt 5)
What makes this easier than the while ( ) loop
for this task?
13Do-While Loop vs. While Loop
- POST-TEST loop (exit-condition)
- The looping condition is tested after executing
the loop body. - Loop body is always executed at least once.
- PRE-TEST loop (entry-condition)
- The looping condition is tested before executing
the loop body. - Loop body may not be executed at all.
14A Special Count Controlled Loop
- The for ( ) statement
- Initialization initializes the LCV
- Condition usually a comparison, acts like a
while ( ) - Incrementing statement LCV is incremented (or
decremented)
for ( initialization test expression update
) 0 or more statements to repeat
15The for ( ) Loop
- Example -- what gets printed?
How did it Happen?
- x gets initialized
- value of x inspected
- x gets incremented
16The for ( ) Loop
- All three of the portions inside the parentheses
can be multiple statements separated by commas - Any or all of the three portions inside the
parenthesis may be missing - accomplish those tasks some other way
- the two semicolons MUST be there
17The break Statement
- We saw it in the switch statement
- Causes immediate exit from innermost block
- switch, while, do-while, for
- Can be used to break out of purposely designed
infinite loop - not a good idea a lazy shortcut
- Use only as last resort to avoid baffling
combinations of multiple Boolean flags and nested
ifs
18The continue Statement
- Valid only in loops
- Terminates current loop iteration
- NOT entire loop
- Causes branch to bottom of loop
- skips rest of loop statements
- Loop then prepares for next iteration
- for () would increment lcv
- all loops would check condition
19Guidelines for Choosing a Looping Statement
- Simple count-controlled
- use for () loop
- Event controlled, body always executed at least
once - use do-while
- Event controlled and nothing known about first
execution - use while, possibly for
- When in doubt gt use while
20Testing and Debugging
- For do-while loops, make sure to try data sets to
make loop go exactly one time - For data-dependant loop where expressions based
on values other than constants - make sure to test for proper number of iterations
- Make sure switch statements have every branch
tested - including default
21Testing and Debugging
- Remember to use break at end of case alternatives
in switch statements - otherwise next case is also executed!
- Case labels in switch statement must be values or
named constants -- no variables - Both switch expression case constant cannot be
floating point - Provide default for switch when possibility of
case values not being matched
22Testing and Debugging
- Make sure all needed switch cases are present
- Choose looping structure carefully
- for( ) loop heading must have two semicolons
-- even if portions omitted - Break statement can exit only one level of
nesting - innermost switch or loop where break is located