Chapter 8 - Repetition Statements - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 8 - Repetition Statements

Description:

Chapter 8 Repetition Statements – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 25
Provided by: RandyA152
Learn more at: http://www.cis.famu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 - Repetition Statements


1
Chapter 8Repetition Statements
2
Introduction
  • Iteration - process of looping or the repetition
    of one or more statements
  • Loop body - the statement, or statements, that
    will be repeated

3
8.1 General Repetition Concepts
  • Two different categories of loops
  • Pre-test Test condition first - complete the
    body of the loop if condition is true
  • Post-test Test condition after body has executed
    once - body will only be repeated if the
    condition is true

4
8.1 General Repetition Concepts

5
8.1 General Repetition Concepts
  • Looping structures must have the following
  • Variable(s) with an initial value(s) to control
    whether the body of the loop is executed (i.e.,
    control variable)
  • Conditional expression involving control
    variable(s)
  • Statement within the body where control variable
    is modified each time the body of the loop is
    executed

6
8.1 General Repetition Concepts
  • Infinite loop a loop that continuously executes
    - the program or loop has to be terminated by the
    user or programmer
  • Usually an error situation
  • Most common cause - failure to manipulate the
    control variable
  • In Windows/Unix - pressing Ctrl C causes
    program to stop execution

7
8.1 General Repetition Concepts
  • Nested loop a loop embedded in another loop
  • Almost any statement type can be the loop body
    including loops or conditional statements
  • Once inner loop finishes, flow transfers to the
    next statement following the inner loop

8
8.2 The while Loop
  • Pre-test loop
  • Syntax
  • while ( ltconditiongt )
  • ltaction/bodygt
  • Action, or body, will continue to execute while
    the condition remains true
  • If the body needs to include multiple statements,
    surround them (action) with curly braces

9
8.2 The while Loop
  • Sometimes see a loop written as while ( 1 )
    ...
  • The numeric literal 1 is treated as a true
    condition and never changes causing an infinite
    loop

10
8.2 The while Loop

char again N' cout ltlt "\nDo you wish to
multiply two numbers (y/n)? " cin gtgt again //
Priming read //- Read two numbers as long as
usery says so. while ( again 'y' again
'Y' ) // Notice no semicolon cout ltlt
"Enter first number " cin gtgt operand1
cout ltlt "Enter second number " cin gtgt
operand2 cout ltlt "Result " ltlt operand1 ltlt
" " ltlt operand2 ltlt " " ltlt operand1
operand2 ltlt endl // Dont forget to change
the control variable cout ltlt "\nDo you wish
to multiply two more numbers (y/n)? " cin
gtgt again // End of the loop body
(action) cout ltlt "The End" ltlt endl
11
8.2 The while Loop
  • Priming read - a read before loop is reached
  • Initializes a variable that can be used to
    control the loop.
  • Initializes a variable being used as an
    accumulator
  • General Concept initialization
  • To provide a correct answer when the loop does
    not execute its body
  • Examples sum 0 min first input value, etc.

12
8.3 The do-while Loop
  • Post-test loop (the body executes at least once)
  • Syntax
  • do
  • ltbody/actiongt
  • while ( ltconditiongt )
  • Note semicolon after ()!!
  • If the body needs to include multiple statements,
    surround them with curly braces

13
8.3 The do-while Loop

char menu_choice float number cout ltlt "Please
enter a number " cin gtgt number do cout ltlt
"\n1) Square the Number\n" ltlt "2) Cube
the Number\n" ltlt "3) Exit\n\n"
ltlt "Please enter menu choice " ltlt endl cin
gtgt menu_choice switch ( menu_choice )
case '1' cout ltlt number ltlt "
Squared " ltlt number number
ltlt endl break case '2'
cout ltlt number ltlt " Cubed "
ltlt number number number ltlt endl
break case '3' cout
ltlt "Goodbye" ltlt endl break
default cout ltlt "Invalid menu
option" ltlt endl while ( menu_choice !
'3' ) // Notice the semicolon
14
8.4 The for Loop
  • Generally used when
  • a specific number of iterations is required
  • The control of the repetition is based on a
    sequence of values (e.g., 1,2,3,,7 odd numbers
    5,4,3,2,1.
  • Both while loops and for loops are pre-test
    loops - could be used interchangeably

15
8.4 The for Loop
  • Syntax (Red for loop specification)
  • for ( ltinitial_value_assignmentgt
  • ltcontinuation_conditiongt
  • ltcontrol_variable_updategt )
  • ltactiongt
  • Example for (k4 k lt 10 k)
  • cin gtgt x

16
8.4 The for Loop

for ( int i 0 i lt 5 i ) cout ltlt i ltlt '
' // Output 0 1 2 3 4
17
8.4 The for Loop
  • for ( ltinitgt
  • ltcontinue_conditiongt
  • ltcontrol_var_updategt )
  • ltbody/actiongt
  • Four sections
  • Loop specification components separated by
  • ltinitgt and ltupdategt components can include
    multiple statements separated by commas
  • body can be any executable statement.

18
8.4 The for Loop
  • Order in which the parts are executed
  • ltinitgt
  • ltcontinuation_conditiongt
  • ltbody/actiongt (if true condition)
  • ltupdategt
  • ltconditiongt
  • ...
  • The ltinitgt section is only executed once

19
8.4 The for Loop
  • Variable(s) may be declared in ltinitgt section
  • Scope is limited to the loop specification and
    body
  • for (int k1 klt 5 k) cout ltlt k ltlt
  • ? output sequence 1 2 3 4

20
8.4 The for Loop

// Example 1 for ( int i 0 i lt 5 i ) // No
semicolon cout ltlt i ltlt endl // Example 2 //
Notice the multiple expressions for ( int i 0,
j 5 i lt 5 i, j-- ) cout ltlt i ltlt ' ' ltlt
j ltlt endl // Example Output // Ex. 1 // Ex. 2
0 0 5 1 1 4 2 2 3
3 3 2 4 4 1
21
8.4 The for loop

int sum 0, value for ( int i 0 i lt 5 i )
// No semicolon cout ltlt "Enter value " ltlt
i 1 ltlt " " cin gtgt value sum
value cout ltlt "The sum of the five values
is " ltlt sum ltlt endl // Example Output Enter
value 1 4 Enter value 2 7 Enter value 3
3 Enter value 4 12 Enter value 5 99 The sum of
the five values is 125
22
8.4.1 Nested for Loops
  • for loops can be nested
  • Used in many algorithms
  • Important when using multi-dimensional arrays

23
8.4.1 Nested for Loops

for ( int row 0 row lt 5 row ) for (
int col 0 col lt 5 col ) cout ltlt col
ltlt ' ' cout ltlt endl // Example Output 0 1
2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
24
8.4.1 Nested for Loops

for ( int row 0 row lt 5 row ) for (
int col row col lt 5 col ) cout ltlt
col ltlt ' ' cout ltlt endl // Example
Output 0 1 2 3 4 1 2 3 4 2 3 4 3 4 4
Write a Comment
User Comments (0)
About PowerShow.com