4) Repetition Control Structure (Part 3)

Embed Size (px)

Citation preview

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    1/15

    CSC 28

    F

    UNDAMENTALS

    OF

    COMPUTERPROBLEMSOLVING

    Topic 4:Repetition Control Structure (Part 3)

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    2/15

    Objectives

    o Nested loop

    o Infinite loop

    o breakand continuestatement

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    3/15

    o A loop (inner loop) as one of the statements in the body of anotherloop (outer loop)

    o Syntax :-

    while (co nd it ion 1) for ( )

    { {

    while (cond it ion 2) for ( )

    { {

    } }

    } }

    Nested Loop

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    4/15

    condition1

    Statement block2

    false

    true

    condition2

    Statement block1

    false

    true

    Nested Loop

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    5/15

    Nested Loop Pseudocode

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    6/15

    o Example 1 :- to print a rectangle

    for (outer=0; outer< 7; outer++)

    { for (inner = 0; inner < 4; inner++)

    cout

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    7/15

    o Example 2 :- to print a multiplication table of series of 1 to 5int row = 1;

    while (row

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    8/15

    Infinite Loop

    A loop is called infinite when the loop repeats itselfrepeatedly with no end.

    int x = 0;

    while (x == 0)

    {

    cout

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    9/15

    How to Stop a Loop

    Known number of iterations beforethe loop stops (for)

    Test for a user-controlled

    conditionbefore or after eachiteration (while,do-while)

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    10/15

    o Break Statemento Causes an exit from

    enclosing loop or switch

    statement.

    o In a nested loop, it will onlystop the loop that it is in.

    o Continue statemento Skipsthe remaining statementsin the loop and proceed with

    the next loop.

    o It is used in a loop structure toalter the flow of control or to

    check for the incorrect data.

    o May only be used in while, forand do-while loops.while (condition)

    {

    statement-1;

    if (condition)break;

    statement-2;

    }

    statement -3

    while (condition)

    {

    statement-1;if (condition)

    continue;

    statement-2;

    }

    statement -3

    breakand continuestatement

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    11/15

    break Example

    int x;

    for (x = 1; x

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    12/15

    continueExample

    int x;

    for (x = 1; x

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    13/15

    Common Loop Errors

    This will lead to an infinite loop!int count = 1;

    while(count

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    14/15

    Common Loop Errors

    Balance may not become equal zero due to numericalinaccuracies!while(balance != 0.0)

    {

    balance = balance - amount;

    }

    Be sure to initialize to 0 a variable used for sums! Be sure to initialize to 1 a variable used for products!while(power

  • 8/11/2019 4) Repetition Control Structure (Part 3)

    15/15

    END

    Thank You