Programming principles: Loops

  • Upload
    abd93

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 Programming principles: Loops

    1/43

    Loops

  • 8/12/2019 Programming principles: Loops

    2/43

    #i ncl ude usi ng namespace st d;i nt mai n( ){

    i nt gr ade;

    cout gr ade;

    swi t ch( gr ade/ 10){case 10:case 9: cout

  • 8/12/2019 Programming principles: Loops

    3/43

    i nt grade; cout gr ade;

    /* can grade be less than zero and greater than 100 simultaneously?*/

    i f( gr ade100) {cout grade;

    }

    swi t ch( gr ade/ 10) {case 10:

    case 9: cout

  • 8/12/2019 Programming principles: Loops

    4/43

    i nt grade; cout gr ade;

    i f( gr ade100) {cout grade; }

    swi t ch( gr ade/ 10) {case 10:case 9: cout

  • 8/12/2019 Programming principles: Loops

    5/43

    i nt grade;

    cout gr ade;

    i f( gr ade100) {cout grade;

    }

    i f( gr ade100)

    {cout grade; }

    i f( gr ade100) {cout grade; }

    / * af t er N i f st at ement , t he swi t ch wi l l st i l l be eval uat ed */

    swi t ch( gr ade/ 10) {. . .

    }

  • 8/12/2019 Programming principles: Loops

    6/43

    Loops (Iterative Constructs) Loops allow a code segment to be

    executed many times.

    Three constructs

    whilestatement

    forstatement

    do-whilestatement

  • 8/12/2019 Programming principles: Loops

    7/43

    The whi l eStatementwhile(condition){

    Action;

    } How it works:

    if conditionis truethen executeAction

    repeat this process untilconditionevaluates to false

    Actionis either a singlestatement or a group ofstatements within braces.

    condition

    Action

    true

    false

  • 8/12/2019 Programming principles: Loops

    8/43

    While Loop Syntax

  • 8/12/2019 Programming principles: Loops

    9/43

    The whi l eLoop

    while(it 's raining)

    {

    }

  • 8/12/2019 Programming principles: Loops

    10/43

  • 8/12/2019 Programming principles: Loops

    11/43Slide 11

    Expression provides an entry condition

    Statement executes if the expression initially evaluates

    to true

    Loop condition is then reevaluated

    Statement continues to execute until the expression isno longer true

    If statement will be executed only once, whilestatement will continue repeating while the condition

    is true.

    The whi l eLoop (continued)

  • 8/12/2019 Programming principles: Loops

    12/43

    i nt grade; cout gr ade;

    whi l e ( gr ade100) {cout grade; }

    swi t ch( gr ade/ 10) {case 10:case 9: cout

  • 8/12/2019 Programming principles: Loops

    13/43

    i nt grade; cout gr ade;

    whi l e ( grade100) {cout

  • 8/12/2019 Programming principles: Loops

    14/43Slide 14

    The whi l eLoop (continued)

    Infinite loop: continues to execute endlessly

    Can be avoided by including statements in theloop body that assure exit condition will

    eventually be false

  • 8/12/2019 Programming principles: Loops

    15/43Slide 15

    Initial Value

    In previous code, the initial value is set by the user

    using cin>>grade;

    While (condition)

    (grade < 0 || grade > 100)

    Update Value

    Infinite loops may generate when you forget to update

    the variable used in the condition

    Three Things to remember

  • 8/12/2019 Programming principles: Loops

    16/43

    i nt grade; cout gr ade;

    whi l e ( grade100) {cout grade; }

    swi t ch( gr ade/ 10) {case 10:case 9: cout

  • 8/12/2019 Programming principles: Loops

    17/43

    Flowchart whi l eLoop

  • 8/12/2019 Programming principles: Loops

    18/43Slide 18

    Two ways to use while loop Sentinel while:

    If you dont know how many times you have toloop

    Use Sentinel value Indicates end of data entry

    Variable is tested in the condition and loop ends whensentinel is encountered

    Sentinel value chosen so it cannot be confused with aregular input (such as -1)

    The whi l eLoop

  • 8/12/2019 Programming principles: Loops

    19/43Slide 19

    Two ways to use while loop counter-controlled while:

    If you know exactly how many times you need torepeat

    Loop repeated until counter reaches a certainvalue

    The whi l eLoop

  • 8/12/2019 Programming principles: Loops

    20/43Slide 20

    counter-controlled while: If you need to loop specific times

    Loop repeated until counter reaches a certainvalue

    counter-controlled while: Example: write a program that calculates the

    average of five grades

    counter-controlled while:

    Example

  • 8/12/2019 Programming principles: Loops

    21/43

    Counter while loop FlowChartStart

    Sum=0Count = 1

    Input

    Grade

    Count

  • 8/12/2019 Programming principles: Loops

    22/43

    i nt mai n( ) {

    i nt grade;

    cout

  • 8/12/2019 Programming principles: Loops

    23/43

    i nt mai n( ) {

    i nt gr ade, Sum=0;

    cout

  • 8/12/2019 Programming principles: Loops

    24/43

    i nt mai n( ) {

    i nt gr ade, Sum=0, Aver age;

    whi l e( ) {cout

  • 8/12/2019 Programming principles: Loops

    25/43

    i nt mai n( ) {

    i nt gr ade, count =1, Sum=0, Aver age;

    whi l e( count

  • 8/12/2019 Programming principles: Loops

    26/43

    i nt mai n( ) {

    i nt gr ade, count =1, Sum=0, Aver age;

    whi l e( count

  • 8/12/2019 Programming principles: Loops

    27/43

    i nt mai n( ) {

    i nt gr ade, count =1, Sum=0, Aver age;

    whi l e( count

  • 8/12/2019 Programming principles: Loops

    28/43

    Sentinel while:

    - Variable is tested in the condition and loop

    ends when sentinel is encountered (we dontknow how many times we have to loop)

    Example:

    write a program that calculates average ofthe grade that the user will enter, the userwill enter negative number to finish

    Sentinel while loop: Example

  • 8/12/2019 Programming principles: Loops

    29/43

    Sentinel while loop FlowChart

    Start

    Sum=0Count = 0

    InputGrade

    Grades>=0

    Sum = Sum + GradeCount = Count + 1

    Average = Sum/Count

    Stop

    No

    Yes

  • 8/12/2019 Programming principles: Loops

    30/43

    i nt mai n( ) {

    i nt gr ade;

    cout

  • 8/12/2019 Programming principles: Loops

    31/43

    i nt mai n( ) {

    i nt gr ade, Sum=0;

    cout >grade;

    return 0; }

    What will happen if variable

    Sum is not initialized ?!

  • 8/12/2019 Programming principles: Loops

    32/43

    i nt mai n( ) {

    i nt gr ade, Sum=0;

    cout >grade; }

    return 0; }

    How many times we will

    loop?!

  • 8/12/2019 Programming principles: Loops

    33/43

    i nt mai n( ) {

    i nt gr ade, Sum=0, Aver age;

    cout >grade; }

    Aver age=Sum/ ; cout

  • 8/12/2019 Programming principles: Loops

    34/43

    i nt mai n( )

    { i nt gr ade, count =0, Sum=0, Average;

    cout >grade;

    }Aver age=Sum/ count ; cout

  • 8/12/2019 Programming principles: Loops

    35/43

    / / Anot her way of wr i t i ng ( sum = sum + grade) and/ / ( count = count + 1)

    i nt mai n( )

    {i nt gr ade, count =0, Sum=0, Average;

    cout >grade; }Aver age=Sum/ count ; cout

  • 8/12/2019 Programming principles: Loops

    36/43

    36

    Compound Assignments Used to update the values of a variable by

    performing an operation on the value that is currentlystored in that variable

    (+=, -=, *=, /=, %=) Note that the assignment comes after the arithmetic

    operators

    Example

  • 8/12/2019 Programming principles: Loops

    37/43

    37

    Compound Assignments Find the output of the following program

  • 8/12/2019 Programming principles: Loops

    38/43

    / / A t hi r d way of wr i t i ng ( count = count + 1)

    i nt mai n( )

    { i nt gr ade, count =0, Sum=0, Average;

    cout >grade;

    }Aver age=Sum/ count ; cout

  • 8/12/2019 Programming principles: Loops

    39/43

    Write a C++ Program that checks if thepassword is correct or not, with

    controlled invalid password entryattempts.

    i t i ( )

  • 8/12/2019 Programming principles: Loops

    40/43

    i nt mai n( ){

    i nt pass, count =1; cout pass;

    whi l e( pass ! = 1234 && count < 3) {

    cout

  • 8/12/2019 Programming principles: Loops

    41/43

    Write a C++ program that convertsinteger grade to letter grade, with

    controlled number of invalid grade entryattempts.

    / / how many updat es you need f or t hi s whi l e?

  • 8/12/2019 Programming principles: Loops

    42/43

    / / o a y upda es you eed o s e

    i nt grade, count =1; cout gr ade;

    whi l e( ( grade100) && count

  • 8/12/2019 Programming principles: Loops

    43/43

    / i f t he condi t i on i s count