C++ Control Technique Repetition

download C++ Control Technique Repetition

of 56

Transcript of C++ Control Technique Repetition

  • 8/6/2019 C++ Control Technique Repetition

    1/56

    BITG 1113:BITG 1113:

    Control TechniqueControl Technique(Repetition)(Repetition)

    LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    2/56

  • 8/6/2019 C++ Control Technique Repetition

    3/56

    3

    Ability to repeat an operation or a series of operations

    manytimes.

    An action or a series of actions.

    C++ has threerepetition

    , or looping structures thatallow us to repeat a set of statements until certain

    conditions are met:

    while

    do-while for

    Why is Repetition Needed ?Why is Repetition Needed ?

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    4/56

    whilewhile LoopingLooping StructureStructure

    4BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    5/56

    5

    whilewhile LoopLoop

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    6/56

    6

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    7/56

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    8/56

    8

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    9/56

    9

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    10/56

    10

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    11/56

    11

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    12/56

    12

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    13/56

    13

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    14/56

    14

    int count = 0;

    while (count < 2)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    15/56

    15

    ControllingControlling whilewhile LoopLoop

    When a program needs to be tested again and again on

    certain data it will operate a large amount of data.

    To overcome the problem, we have four cases:

    Counter controlled while loops Sentinel controlled while loops

    Flag controlled while loops

    User controlled while loops

    EOF-Controlled while loops

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    16/56

    16

    CounterCounter--controlledcontrolled whilewhile loopsloops

    We know how many pieces of data need to be read.

    Use a counter, by initializing the value to 0.

    Ifcounter < N, the body of the while statement

    executes until the value ofcounter >= N

    The structure :

    counter = 0; //initialize the loop control variablewhile (counter < N) //test the loop control variable{

    counter ++//update the loop control variable

    }

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    17/56

    17

    ExampleofaExampleofawhilewhile loop : Counterloop : Counter--controlledcontrolled

    BITG 1113 : LECTURE 5

    #include

    using namespace std;

    int main ()

    {

    double score, sum=0,average;

    int studProc;

    studProc = 0; //initialization

    while (studProc < 10) //condition

    {

    cout >score;

    sum += score;

    studProc++; //updating

    }

    average = sum/10;

    cout

  • 8/6/2019 C++ Control Technique Repetition

    18/56

  • 8/6/2019 C++ Control Technique Repetition

    19/56

    19

    We do not know how many data needs to be read

    .

    The last entry called sentinel, is a special value.

    A sentinel value is a value that is not a legitimate data value

    for a particular problem (but is of proper type) that is used

    as a stopping value.

    The while loop continues to execute as long as the

    program has not read the sentinel.

    General structure :cin >> variable;

    while (variable != -1){

    cin>>variable;

    }

    SentinelSentinel--ControlledControlled whilewhile loopsloops

    Sentinel Value

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    20/56

    20

    ExampleofaExampleofawhilewhile loop : Sentinelloop : Sentinel--controlledcontrolled

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    21/56

    21

    ExampleofaExampleofawhilewhile loop : Sentinelloop : Sentinel--controlledcontrolled

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    22/56

    22

    Uses a user predefined variable to control the loop

    User ControlledUser Controlled whilewhile LoopsLoops

    char continueLoop = 'Y';

    while (continueLoop == 'Y')

    {

    // Execute body once// Prompt the user for confirmation

    cout > continueLoop;

    }

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    23/56

    23

    ExampleofaExampleofawhilewhile loop : Userloop : User--controlledcontrolled

    BITG 1113 : LECTURE 5

    #include

    using namespace std;

    int main ()

    {

    int count=0;

    double score, sum=0,average;

    char answer=y; //initialization

    while (answer==y|| answer==Y) //condition

    { cout >score;

    sum += score;

    count++;coutanswer; //updating

    }

    average = sum/count;

    cout

  • 8/6/2019 C++ Control Technique Repetition

    24/56

    24

    Output

    Enter your score:60

    Do you want to continue?y/Y for Yes:y

    Enter your score:50

    Do you want to continue?y/Y for Yes:y

    Enter your score:90

    Do you want to continue?y/Y for Yes:y

    Enter your score:80

    Do you want to continue?y/Y for Yes:yEnter your score:55

    Do you want to continue?y/Y for Yes:n

    The average is :67

    ExampleofaExampleofawhilewhile loop : Userloop : User--controlledcontrolled

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    25/56

    25

    Uses a bool variable to control the loop Example :

    found =false;//initialize the loop controlvariable

    while (!found) //test the loop control variable{

    if (expression)found = true //re-initialize the loop control

    variable

    }

    FlagFlag--ControlledControlled whilewhile LoopsLoops

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    26/56

    ExampleofaExampleofawhilewhile loop : Flagloop : Flag--controlledcontrolled

    #include

    using namespace std;

    int main()

    {

    char letter;

    bool found = false;//initializationwhile (!found) //condition

    {

    coutletter;

    if (letter=='x')

    {found = true; //updating

    cout

  • 8/6/2019 C++ Control Technique Repetition

    27/56

    Output

    Enter a letter. Press x to stop : s

    Enter a letter. Press x to stop : x

    STOP

    27

    ExampleofaExampleofawhilewhile loop : Flagloop : Flag--controlledcontrolled

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    28/56

    28

    Is used when the data file is frequently altered, added ordeleted

    Example :

    cin >> variable; //initialize the loop control

    variable

    while (cin)

    {

    cin >> variable; //re-initialize the loop

    control variable

    }

    The loop stops when the system detects the end-of-file

    signal( by pressing ).

    EOFEOF--ControlledControlled whilewhile LoopsLoops

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    29/56

    #include

    using namespace std;

    int main( )

    {

    int n = 0;

    float sum = 0;

    cout n)

    sum += n;cout

  • 8/6/2019 C++ Control Technique Repetition

    30/56

    Output :

    Enter your numbers:^Z to stop.

    1 2 3 4 5 ^Z

    SUM = 15

    Press any key to continue . . .

    30

    ExampleofaExampleofawhilewhile loop :EOFloop :EOF--controlledcontrolled

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    31/56

    dowhiledowhile Looping StructureLooping Structure

    31BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    32/56

    dowhiledowhile LoopLoop

    32BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    33/56

    dowhiledowhile LoopLoop

    33

    #include

    using namespace std;

    int main()

    {

    int a; //initialization

    do

    {

    cout > a; //updating

    } while (a < 10 || a > 20); //condition

    cout

  • 8/6/2019 C++ Control Technique Repetition

    34/56

    Output :Output :

    To stop enter a number between 10 & 20 : 3

    To stop enter a number between 10 & 20 : 10

    END

    34BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    35/56

    forfor Looping StructureLooping Structure

    35BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    36/56

    forfor LoopLoop

    36BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    37/56

    37

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    38/56

    38

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    39/56

    39

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    40/56

    40

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    41/56

    41

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    42/56

    42

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    43/56

    43

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    44/56

    44

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    45/56

    45

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    46/56

    46

    TraceTrace forfor Loop, Cont.Loop, Cont.

    int i;

    for (i = 0; i < 2; i++)

    {

    cout

  • 8/6/2019 C++ Control Technique Repetition

    47/56

    ComparingComparing forfor andand whilewhile LoopsLoops

    47BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    48/56

    ComparingComparing forfor andand whilewhile LoopsLoops

    48

    int sum = 0;

    intint ii = 1= 1;

    while (ii a;

    sum += a;

    ii++++;

    }

    sum = 0;

    for(intint ii = 1= 1; ii a;

    sum += a;

    }

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    49/56

    49

    intint ii = 2= 2;

    while (ii

  • 8/6/2019 C++ Control Technique Repetition

    50/56

    50

    RecommendationsRecommendations

    Use the one that is most intuitive and comfortable for you.

    In general, a for loop may be used if the number of

    repetitions is known, as, for example, when you need to

    print a message 100 times.

    Awhile loop may be used if the number ofrepetitions is

    not known, as in the case of reading the numbers until the

    input is 0.

    Ado-while loop can be used to replace a while loop if

    the loop body has to be executed before testing thecontinuation condition.

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    51/56

    NestedNested forfor LoopLoop

    51

    // Print numbers on a line

    #include #include

    int main (void)

    {

    for (int i = 1; i

  • 8/6/2019 C++ Control Technique Repetition

    52/56

    52

    Other Statement Related to LoopingOther Statement Related to Looping

    break

    causes a loop to terminate

    terminate only the inner loop in nested loop

    continue

    transfers to the testing expression in while and

    dowhile statement

    transfers to the updating expression in a for

    statement.

    BITG 1113 : LECTURE 5

    ThTh ii SS

  • 8/6/2019 C++ Control Technique Repetition

    53/56

    53

    while (limit test)

    {

    continue;

    }

    do

    {

    continue;

    } while (limit test)

    for (initialize;limit test;update)

    {

    continue;

    }

    TheThe continuecontinue StatementStatement

    BITG 1113 : LECTURE 5

  • 8/6/2019 C++ Control Technique Repetition

    54/56

    TheThe breakbreak ExampleExample

    54

    int main ( )

    {int x = 5 ;

    while ( x < = 20)

    {

    if (x == 10 )

    break;

    cout

  • 8/6/2019 C++ Control Technique Repetition

    55/56

    TheThe continuecontinue ExampleExample

    55

    #include

    using namespace std;

    int main( )

    {

    int count = 0;

    int n = 0;

    float sum = 0;

    cout n)

    {

    if (n == 0)

    continue;

    sum += n;

    count++;

    }

    cout

  • 8/6/2019 C++ Control Technique Repetition

    56/56

    OutputOutput

    Enter some numbers. Press ^Z to stop.

    2

    0

    3

    ^Z

    Sum/Count = 2.5

    56BITG 1113 LECTURE 5