ekt120 week04 loop

Upload: muhammad-faiz-bin-ahmad-shafi

Post on 03-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Ekt120 Week04 Loop

    1/25

    EKT 120 SEM II 09/10 1

    Week 4Repetition Structures /Loops

  • 8/12/2019 Ekt120 Week04 Loop

    2/25

    EKT 120 SEM II 09/10 2

    Outline Introduction

    While loops

    Three types of while loops

    Do-while loops

    For loops Nested loops

  • 8/12/2019 Ekt120 Week04 Loop

    3/25

    EKT 120 SEM II 09/10 3

    Why Need Loops ? Suppose we want to add five numbers and

    find the average.

    From what you have learned so far, you couldproceed as followsscanf(%d %d %d %d %d, &num1, &num2,

    &num3, &num4, &num5);

    sum = num1 + num2 + num3 + num4 + num5;average = sum / 5;

    If 100 numbers, 1000 numbers?

  • 8/12/2019 Ekt120 Week04 Loop

    4/25

    EKT 120 SEM II 09/10 4

    Repetition (Loop) Used to control the flow of a program Loops are basically repetitions or iterations used to

    repeat a segment of code

    Three statements can be used to implement loops inC whilestatement do-whilestatement forstatement

    whileand forstatements are similar inimplementation, but have different syntax

    The do-whilestatement is different - the followingcode is executed at least once

  • 8/12/2019 Ekt120 Week04 Loop

    5/25

    EKT 120 SEM II 09/10 5

    The whileloop structure The general form of the whilestatement is:

    while (expression)statement;

    To avoid an infinite loop, make sure thatthe loops body contains statement (s) thatassure that the exit condition i.e.theexpression in the while statement will

    eventually be false.whileloop repeates until condition becomesfalse

  • 8/12/2019 Ekt120 Week04 Loop

    6/25

    EKT 120 SEM II 09/10 6

    Flowchart of a whilestatement

  • 8/12/2019 Ekt120 Week04 Loop

    7/25

    EKT 120 SEM II 09/10 7

    The whileloop structure-cont There are basically three types of while

    loops:

    Counter-controlled whileloop

    Sentinel-controlled whileloop

    Flag-controlled whileloop

  • 8/12/2019 Ekt120 Week04 Loop

    8/25

    EKT 120 SEM II 09/10 8

    Counter-Controlled while

    Loops Definite repetition: know how many times loop will execute

    Control variableused to count repetitions

    Example:int counter = 1; // declare and initializewhile ( counter

  • 8/12/2019 Ekt120 Week04 Loop

    9/25

    EKT 120 SEM II 09/10 9

    Counter-Controlled while

    LoopsAnother example:

    int product = 2;

    while ( product

  • 8/12/2019 Ekt120 Week04 Loop

    10/25

    EKT 120 SEM II 09/10 10

    Sentinel-Controlled while

    Loops Indefinite repetition

    Used when number of repetitions not known

    and loop needs to input value repeatedly foreach iteration

    Sentinel valueindicates "end of data

  • 8/12/2019 Ekt120 Week04 Loop

    11/25

    EKT 120 SEM II 09/10 11

    Sentinel-Controlled while

    Loops-continuedExample:int number, count, sum;sum = 0;count = 0;

    printf(To stop enter -999. Enter positive numbers : " );scanf(%d, &number);while (number != -999){

    sum = sum + number; //find sum of numbers enteredcount++; //count how many numbers entered

    printf(To stop enter -999. Enter positive numbers : " );

    scanf(%d, &number);}

    printf(\nThe sum of %d numbers is %d, count, sum);

    Requirement:1. Readcontrolvariablevalue

    before enter loop2. A condition thattestscontrolvariables validity(i.e., whetherlooping should

    continue)3. Read againcontrol variablebefore end of loop

    Sentinel value

  • 8/12/2019 Ekt120 Week04 Loop

    12/25

    EKT 120 SEM II 09/10 12

    Sentinel-Controlled while

    Loops-continued Another example:

    char choice;

    printf(Continue? y-yes n-no: );

    scanf(%c, &choice);while ( choice == y)

    { printf(\nEnter grade point:);

    scanf(%f, &grade_pt);

    if(grade_pt > 2.0)

    printf(\nPass);printf(Continue? y-yes n-no: );

    scanf(%c, &choice);

    }

    read control variable

    test condition

    read again

  • 8/12/2019 Ekt120 Week04 Loop

    13/25

    EKT 120 SEM II 09/10 13

    Flag-Controlled whileLoops Uses a boolean variableto control the loop.

    Loop exit when expression is evaluated to false.

    bool found = false;

    while (!found){ printf(Enter number between 1 and 200:);

    scanf(%d, &guess);if ((guess>= 88) &&(guess

  • 8/12/2019 Ekt120 Week04 Loop

    14/25

    EKT 120 SEM II 09/10 14

    The do-whileRepetition

    Structure Similar to thewhilestructure Condition for repetition is tested afterthe

    body of the loop is performed All actions are performed at least once Expression can be written as count-controlled

    or sentinel-controlled Format:

    do {statement;

    } while ( condition);

  • 8/12/2019 Ekt120 Week04 Loop

    15/25

    EKT 120 SEM II 09/10 15

    Flowchart of a do-while

    structure

    true

    false

    action(s)

    condition

  • 8/12/2019 Ekt120 Week04 Loop

    16/25

    EKT 120 SEM II 09/10 16

    The do-whileRepetition

    Structure

    Example : prints the integers from 1to 10

    int counter = 1;

    do {

    printf( "%d ", counter );

    } while (++counter 2.0)printf(\nPass);

    printf(Continue?y-yes n-no :);

    scanf(%c, &choice);

    }while ( choice == y)

    count-controlled

    sentinel-controlled

  • 8/12/2019 Ekt120 Week04 Loop

    17/25

    EKT 120 SEM II 09/10 17

    The do-whileRepetitionStructure

    To avoid an infinite loop, make sure that the loop body contains astatement that ultimately makes the expression false and assures thatit exits

    Another example:

    int i = 0;

    do{

    printf (%d ,i);i = i + 5;

    } while (i

  • 8/12/2019 Ekt120 Week04 Loop

    18/25

    EKT 120 SEM II 09/10 18

    The do-whileLoopingStructure (Example)

    Example: Consider the following two loops

    printf (%d ,i); printf (%d ,i);

  • 8/12/2019 Ekt120 Week04 Loop

    19/25

    EKT 120 SEM II 09/10 19

    The do-whileLoopingStructure (Example)

    In (a), the whileloop, produces nothing

    In (b) the do...whileloop, outputs the

    number 11

  • 8/12/2019 Ekt120 Week04 Loop

    20/25

    EKT 120 SEM II 09/10 20

    The forRepetition Structure

    Format when using forloopsfor(initialization ; loop continuation test ; incrementstatement)

    Use forloops when already know howmany times to repeat

    Example:

    for(counter = 1;counter

  • 8/12/2019 Ekt120 Week04 Loop

    21/25

    EKT 120 SEM II 09/10 21

    The forRepetition Structure

    for loops can usually be rewritten as while loops:

    initialization;while( loop continuation test)

    {

    statement;increment statement;

    }

    Initialization and increment

    Can be comma-separated lists

    Example: for(int i=0,j=0;j+i

  • 8/12/2019 Ekt120 Week04 Loop

    22/25

    EKT 120 SEM II 09/10 22

    Flow of a forstatement

  • 8/12/2019 Ekt120 Week04 Loop

    23/25

    EKT 120 SEM II 09/10 23

    Nested loop

    Loop within a loop

    Inner loop is performed first

    e.g.

    for(i=1;i

  • 8/12/2019 Ekt120 Week04 Loop

    24/25

    EKT 120 SEM II 09/10 24

    Nested loop

    Another example: Program finds and prints avg of three test scores, then asks

    whether user wants to continue

    do{

    for(count=0; count

  • 8/12/2019 Ekt120 Week04 Loop

    25/25

    EKT 120 SEM II 09/10 25

    End Week 4 - Loops

    Q & A!