12_-_iteration.doc

Upload: bianca-jane-maaliw

Post on 04-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 12_-_Iteration.doc

    1/11

  • 8/14/2019 12_-_Iteration.doc

    2/11

    count+emp -) /"nitiali0ation or loop control variable/while &count+emp 1 2' ( /$esting. 3oop repetition condition/ printf&45ours6 7') scanf&48d7, 9hours') printf&4ate6 7') scanf&48lf7, 9rate') pay hours / rate) printf&4;ay is n7, pay') count+emp count+emp ? ) /@pdating. count+emp is updated./*printf&4>nAll employees processed>n7')

    Example:

    Infinite loops

    $he programmer must always ma#e sure that the conditional expression in a

    whileloop must sooner or later become false. therwise, the program willenter in an infinite loop. "n other words, the loop will not terminate.

    $he body of the loop changes something in the condition so that eventually

    the condition become false and the program will continue with the statementsthat follow the while loop.

    Example:"n this example, the conditional expression ofthe whileloop &counter 1 -' will neverbecome false since counterwill always beequal to .

    Page 2

    sum -)counter )

    while &counter 1 -' (sum ? )

    *

    sum -)counter )while &counter 1 -' (

    sum ? )

    counter ? )*

  • 8/14/2019 12_-_Iteration.doc

    3/11

  • 8/14/2019 12_-_Iteration.doc

    4/11

    Practice 3:$he Hu ipot company pays it salesmen on a commission basis it pays

    !-8 for sales exceeding ;!-,---.--, 8 for sales exceeding ;,---.--, and%8 for sales below or equal ;,---.--. Create a C program that will input

    unit price and quantity sold and then output the gross sales &unit price /quantity', and commission&sales / ;ercentage' based from the sales of eachsalesman. After computing a commission for a particular salesman, theprogram must prompt the user to compute for another commission. "f the userwants to stop processing the commission, output the sum of all gross payotherwise continue to compute commission.

    break Statement $o interrupt the normal flow of control within a loop.

    $he breakstatement causes an exit from an enclosing loop.

    "n other words, encountering the breakstatement causes immediate

    termination of the loop.

    Example 1: Example 2:

    for Statement

    % loop control componentsI initiali0ation of the loop controlvariable

    Page 4

    a)b)while&a'( if&b'( printf&4All done J bye.>n7') brea#)

    *printf&4Kot yet done.>n7')*

    Linclude 1stdio.h6main & '(int x)while &' (

    printf &M>nNuess the secret

    number 6 M')scanf &48d7, 9x')if &x '

    brea#)else

    printf &M>nOorryBBBM')*printf&M>n>nCongratulationsBBBM')

    *

  • 8/14/2019 12_-_Iteration.doc

    5/11

    I test of the loop repetition conditionI change &update' of the loop control variableAn important feature of the forstatement in C is that it supplies a designatedplace for each of these three components.

    Format 1:

    Format 2:

    Example

    PracticePage 5

    for &initiali0ation expression) loop repetition condition) update expression' statement)statement!)for &initiali0ation expression) loop repetition condition) update expression'( statement) statement!)*statement%)

    total+pay -.-)for &count+emp -) / initiali0ation /

    count+emp 1 2) / loop repetition condition / count+emp ? ' ( / update/ printf&45ours6 7') scanf&48d7, 9hours') printf&4ate6 7') scanf&48lf7, 9rate') pay hours / rate)

    printf&4;ay is n7, pay') total+pay total+pay ? pay)*printf&4>nAll employees processed>n7')printf&4$otal payroll is n7, total+pay')

  • 8/14/2019 12_-_Iteration.doc

    6/11

    for mult -)mult 1 --)mult ? )printf&48d>n7, mult')

    . $race the execution of the loop that follows for nP. Ohow values of odd andsum after the update of the loop counter for each iteration.

    !. Dh

    at

    errors do you see in the followingfragment Correct the code so it displays all multiples of from - through--.

    %. $race the following program fragment:

    G. Drite a program to display an inchesJtoJcentimeters conversion table. $he smallestand largest number of inches in the table areinput values. Qour table should giveconversion in =Jinch intervals. ne inch

    equals !.G cm.. Convert the following statement usingthe forstatement and determine theoutput:

    Endless forloop

    or

    Page 6

    sum -)for &odd) odd1n) odd ?!'

    sum sum ? odd)printf&4Oum of positive odd numbers less than 8d is 8d.>n7, n, sum')

    -)for &i ) i1) i??'( printf&48d 8d>n7, ", ') J !)*

    ctr )while &ctr 1 -' (

    printf&4Otill countingR7')printf&48d.>n7, ctr')ctr??)

    *

    for & ) ) ' ( statement)*

    for&i- ) ) ' ( statement) statement!)*

    for & ) ) ??' printf &45elloB>n7')

  • 8/14/2019 12_-_Iteration.doc

    7/11

    "n this example, the forstatement does not have a test condition. $herefore,there is no condition for the loop to terminate.

    "n this example, the forstatement does not

    have a loop operation. $herefore, the test conditionwill never become false.

    do-while Statement

    Soth the for statement and the while statement evaluate a loop repetition

    condition before the first execution of the loop body.

    "n most cases, this pretest is desirable and prevents the loop from executingwhen there may be no data items to process or when the initial value of theloop control variable is outside the expected range.

    $here are some situations, generally involving interactive input, when we

    #now that a loop must execute at least one time.

    Format 1:

    Format 2:

    Example

    Page 7

    for & ) 1 -) ' printf &45elloB>n7')

    do statement)while &loop repetition condition')

    do ( statement) statement!) satementn)* while &loop repetition condition')

    total+pay -.-)count+emp -) / initiali0ation /

    do(printf&45ours6 7') scanf&48d7, 9hours') printf&4ate6 7') scanf&48lf7, 9rate') pay hours / rate) printf&4;ay is n7, pay') count+emp ? ) / update/ total+pay total+pay ? pay)* while &count+emp 1 2') / loop repetition condition /printf&4>nAll employees processed>n7')printf&4$otal payroll is n7, total+pay')

  • 8/14/2019 12_-_Iteration.doc

    8/11

    do while vs. while

    while continuation condition is tested at the beginning of the execution.

    do while continuation condition is tested at the end.

    while loop can be executed 0ero times

    do while must be executed at least once.

    Practiceewrite the following code using a doJwhile statement with no decisions in theloop body:

    continue Statement $o interrupt the normal flow of control within a loop, the programmer can use

    also continuestatement.

    Page 8

    sum -)

    for &odd) odd1n) odd ?!' sum sum ? odd)printf&4Oum of positive odd numbers less than 8d is 8d.>n7, n, sum')

  • 8/14/2019 12_-_Iteration.doc

    9/11

  • 8/14/2019 12_-_Iteration.doc

    10/11

  • 8/14/2019 12_-_Iteration.doc

    11/11

    Enter a number: /////Enter a number: /Enter a number: -

    U. Enter a number between ! and !- and print a filled square with sides of thatnumber of asteris#s &/'.

    Example:Enter a number: %//////

    ///

    -. Enter a number and print out its multiplication table from to -.Example:

    Enter a number: % times % %! times % =

    .

    .- times % %-

    -. Enter your name and a number and print that number of copies of yourname.

    Example:

    Enter your name: FredEnter a number: GFredFredFredFred

    Page 11