Lecture 05 Loops 2014

download Lecture 05 Loops 2014

of 10

Transcript of Lecture 05 Loops 2014

  • 8/9/2019 Lecture 05 Loops 2014

    1/25

    IT101, Semester 1Lecture 5

  • 8/9/2019 Lecture 05 Loops 2014

    2/25

    Repetition Structure

  • 8/9/2019 Lecture 05 Loops 2014

    3/25

    Types of Repetition Structures in C++

    while

    for

    do-while

    3

  • 8/9/2019 Lecture 05 Loops 2014

    4/25

    Pre-test and Post-test loops

    Pre-test loops are entrance controlled loops. You execute the loop od! after e"aluatin# thetest.Loop od! can e executed $ero or more times.

    Post-test loops are exit controlled loops.

    You test the loop after executin# the entire loopod!.

    Loop od! can e executed one or more times.

    4

  • 8/9/2019 Lecture 05 Loops 2014

    5/25

    The While Loop Syntax of While Loop

    while (boolean expr)statement;

    next statement;

    while (boolean expr){

    statement 1;statement 2;

    }next statement;

    5

    booleanexpr

    next statementstatements

    false

    true

  • 8/9/2019 Lecture 05 Loops 2014

    6/25

  • 8/9/2019 Lecture 05 Loops 2014

    7/25

    &xample- Print 'um ersfrom 1 to 10int count (1)while *count +( 10

    cout++ count ++endl)count )

    /

    7

  • 8/9/2019 Lecture 05 Loops 2014

    8/25

    Infinite Loops

    %n in nite loop is one in which thecondition is initiall! satis ed, so the loop isentered, ut the condition for exitin# theloop is ne"er met.

    enerall! an in nite loop is caused !failin# to modif! a "aria le in"ol"ed in thecondition within the loop od!. To rea2 out of a malfunctionin# pro#rampress ctrl-3 on Linux or ctrl- rea2, on an4 S or 6indows machine.

    8

  • 8/9/2019 Lecture 05 Loops 2014

    9/25

    What Does This Loop Print

    int I =1; while(I < 6 )

    cout

  • 8/9/2019 Lecture 05 Loops 2014

    10/25

    Interacti!e I"# and Loopin$

    The sentinel or trailer techni9ue uses aspecial end-of data "alue to indicate the endof meanin#ful data.:sin# the while loop, we place an inputstatement efore the loop to read the rst"alue and an input statement at the ottom ofthe loop to read the next "alue.

    The loop condition tests that the "alue is note9ual to the sentinel "alue.

    10

  • 8/9/2019 Lecture 05 Loops 2014

    11/25

    %n &xample of Sentinel Input

    Input a list of positi"e num ers from the2e! oard and nd the a"era#e. The list isterminated with the "alue -;;.

    11

  • 8/9/2019 Lecture 05 Loops 2014

    12/25

    #include using names ace std;int main()!

    int number" sum" cnt; Initiali$ationcout

  • 8/9/2019 Lecture 05 Loops 2014

    13/25

    Counter 'Controlled Loops

    A counter controlled loop is a looping controlstructure in which a loop variable manages the

    repetition by counting!he syntax for a counter controlled loop in " and "## is$for(inti expr; boolean expr; increment expr)

    statement;

    A loop body of more than one statement% must beenclosed in curly braces

    13

  • 8/9/2019 Lecture 05 Loops 2014

    14/25

  • 8/9/2019 Lecture 05 Loops 2014

    15/25

    &xamplePrint the num ers from 100 to 10 as follows)100;0 (10 ) I ( i-10

    cout++ i ++ ? ? )/

    15

  • 8/9/2019 Lecture 05 Loops 2014

    16/25

    Comments on the for loop

    %ll three expressions that are part of the forloop are optional. The semicolons are notoptional.If the oolean expression is omitted !ou ha"ean in nite loop.:se the for loop when !ou 2now exactl! howman! times the loop od! is to e executed,either as a speci c "alue or as an expression.

    16

  • 8/9/2019 Lecture 05 Loops 2014

    17/25

    %ll parts of the for loop areoptional

    i = *;&or(; i

  • 8/9/2019 Lecture 05 Loops 2014

    18/25

    )*rea , and )continue, Statements ith Loops

    The rea2 statement causes theimmediate termination of the execution of

    the loop od! and the continuation ofexecution with the rst statement after theloop.

    The continue statement causes theimmediate termination of the execution ofthe loop od! ut not the exitin# of the

    loop. 18

  • 8/9/2019 Lecture 05 Loops 2014

    19/25

    Nestin$ of Loops

    The statements in the loop od! ma! e an!3 statement includin# another loopin#statement.6hen a for loop is entered from the top, theinitiali$ation occurs and then the ooleanexpressions are executed.

    6hen it is entered as a result of completin#execution of the loop od! the incrementand then the oolean expressions areexecuted.

    19

  • 8/9/2019 Lecture 05 Loops 2014

    20/25

    Nested (or Loops

    &or (I=1; I

  • 8/9/2019 Lecture 05 Loops 2014

    21/25

    Loop ith Post test .yntax to /o0 hileoop

    do

    statement; while (bool e0 r);

    do!

    statement 1;statement ;

    while (bool e0 r); 21

    statement

    boolexpr

    3ext statement

    true false

  • 8/9/2019 Lecture 05 Loops 2014

    22/25

    %n example of the Do While Loop

    int i=1;do! cout

  • 8/9/2019 Lecture 05 Loops 2014

    23/25

  • 8/9/2019 Lecture 05 Loops 2014

    24/25

  • 8/9/2019 Lecture 05 Loops 2014

    25/25