Additional Control Structures - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Additional Control Structures

Description:

Additional Control Structures Chapter 9 – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 23
Provided by: S982
Category:

less

Transcript and Presenter's Notes

Title: Additional Control Structures


1
Additional Control Structures
  • Chapter 9

2
New and Improved . . .
3
Branching Statements
  • Recall the current branching capability provided
    by the if ( ) statement
  • Only branches twoways
  • What if we desiremultiway branching?

4
Multiway 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
()
5
Switch 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
()
6
Switch 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
()
7
Switch 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
8
A 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

9
The Do-While Statement
  • Condition tested at end/bottom of loop
  • Guarantees loop body executes at least once

10
The do-while Illustrated
Note alwaysat least oneiteration
Loop Iteration
Loop Entry
Loop Exit
Loop Trest
11
The Do-While Statement
  • Can be used for a counting loop

What gets printed??
How do you change it to go 10 times?
12
The 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?
13
Do-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.

14
A 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
15
The for ( ) Loop
  • Example -- what gets printed?

How did it Happen?
  • x gets initialized
  • value of x inspected
  • x gets incremented

16
The 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

17
The 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

18
The 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

19
Guidelines 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

20
Testing 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

21
Testing 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

22
Testing 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
Write a Comment
User Comments (0)
About PowerShow.com