Hanly Ppt005

74
Chapter 5 Repetition and Loop Statements

description

loop

Transcript of Hanly Ppt005

Chapter 5Repetition and Loop Statements5-2Outline5.1 REPETITION IN PROGRAMS5.2 COUNTING LOOPS AN T!E "!ILE STATEMENT5.# COMPUTING A SUM OR A PROUCT IN A LOOP5.$ T!E %OR STATEMENT5.5 CONITIONAL LOOPS5.& LOOP ESIGN5.' NESTE LOOPS5.( T!E O)"!ILE STATEMENT AN %LAG)CONTROLLE LOOPS5.* PRO+LEM SOL,ING ILLUSTRATE- CASE STUDY: COMPUTING RADIATION LEVELS5.1. !O" TO E+UG AN TEST PROGRAMS5.11 COMMON PROGRAMMING ERRORS5-3/Three t0pes o1 pro2ram str34t3re- sequence- selection- repetition This 4hapter/The repetition o1 steps in a pro2ram is 4a55ed loop/This 4hapter dis43ss three C 5oop 4ontro5 statement- while- for do-while5-45.1 Repetition in programs/Loop-A 4ontro5 str34t3re that repeats a 2ro3p o1 steps in a pro2ram/Loop body-The statements that are repeated in the 5oop/Three 63estions to determine 7hether 5oops 7i55 8e re63ired in the 2enera5 a52orithm9 (Figure5.1)1. "ere there an0 steps I repeated as I so5:ed the pro85em; I1 so< 7hi4h ones;2. I1 the ans7er to 63estion 1 is 0es< did I =no7 in ad:an4e ho7 man0 times to repeat the steps;#. I1 the ans7er to 63estion 2 is no< ho7 did I =no7 ho7 5on2 to =eep repeatin2 the steps; 5-5%i23re 5.1%5o7 ia2ram o1 Loop Choi4e Pro4ess5-6Ta85e 5.1 Comparison o1 Loop >inds ?Pa2e 211@>ind "hen Used C Imp5ementation Str34t3reSe4tion Containin2 an EAamp5eCo3ntin2 5oopwhilefor5.25.$Sentine5)4ontro55ed 5oopwhile, for5.&End1i5e)4ontro55er 5oopwhile, for5.&Inp3t :a5idation 5oopdo-while5.(Genera5 4onditiona5 5oopwhile, for5.5< 5.*5-75.2 Counting Loops and the while Statement/Co3nter)4ontro55ed 5oop ?4o3ntin2 5oop@-A 5oop 7hose re63ired n3m8er o1 iterations 4an 8e determined 8e1ore 5oop eAe43tion 8e2ins/%i23re 5.2 sho7s a pro2ram 1ra2ment that 4omp3tes and disp5a0s the 2ross pa0 1or se:en emp5o0ees.5-8%i23re 5.2Pro2ram %ra2ment 7ith a Loop5-9Flowhart !or a while LoopLoop repetition conditionstatement5-10"he while Statement/Loop repetition 4ondition-The 4ondition that 4ontro5s 5oop repetition/Loop 4ontro5 :aria85e-The :aria85e 7hose :a53e 4ontro5s 5oop repetition/The 5oop 4ontro5 :aria85e m3st 8e-Initia5iBation-Testin2-Updatin25-11Synta# o! the while Statement/S0ntaA while (loop repetition condition)statement/EAamp5e CD isp5a0 N asteris=s. DC4o3ntEstar F . 7hi5e ?4o3ntEstar G N@ Hprint1?IDJ@ 4o3ntEstar F 4o3ntEstar K 1 L5-125.$ Computing a Sum or a %rodut in a Loop/a443m35ator-a :aria85e 3sed to store a :a53e 8ein2 4omp3ted in in4rements d3rin2 the eAe43tion o1 a 5oop/%i2.5.$ Pro2ram to Comp3te Compan0 Pa0ro555-13%i23re 5.$Pro2ram to Comp3te Compan0 Pa0ro555-14%i23re 5.$Pro2ram to Comp3te Compan0 Pa0ro55 ?4ontMd@5-15Compound &ssignment Operators/Assi2nment statements o1 the 1orm-:aria85e F :aria85e op eApression/"here op is a C arithmeti4 operator :aria85e op F eApression-Ta85e 5.#EA. time F time - 1N time '( 1N5-16Compound &ssignment OperatorsSimple &ssignment OperatorsCompound &ssignment Operatorscount_emp = count_emp + 1;count_emp += 1;time = time -1; time -= 1;product = product * item;product *= item;total = total / number;total /= number;n = n % (x+1); n %= x+1;5-175.) "he for Statement/Three 5oop 4ontro5 4omponents-Initia5iBation o1 the 5oop :aria85e-Test o1 the 5oop repetition 4ondition-Chan2e ?3pdate@ o1 the 5oop 4ontro5 :aria85e/Usin2 a 1or statement in a 4o3ntin2 5oop. ?%i23re 5.5@ 5-18%i23re 5.5Usin2 a 1or Statement in a Co3ntin2 Loop5-19Synta# o! for Statement/S0ntaA9 !or (initiali*ation e#pression+loop repetition ondition+update e#pression)statement/EAamp5e9CD isp5a0 N asteris=s DC1or ?4o3ntEstar F .N 4o3ntEstar GNN 4o3ntEstar KF 1@print1?IDJ@N5-20,nrement and -erement Operators/Side e11e4t-a 4han2e in the :a53e o1 a :aria85e as a res35t o1 4arr0in2 o3t an operation5-21%i23re 5.&Comparison o1 Pre1iA and Post1iA In4rements5-22.#ample 5.$/%3n4tion !ctori!l 4omp3tes the 1a4toria5 o1 an inte2er represented 80 the 1orma5 parameter n. The 5oop 8od0 eAe43tes 1or de4reasin2 :a53es o1 i 1rom n thro32h 2< and ea4h :a53e o1 i is in4orporated in the a443m35atin2 prod34t. Loop eAit o443rs 7hen i is 1. ?%i23re 5.'@5-23%i23re 5.'%3n4tion to Comp3te %a4toria55-24,nrements and -erements Other "han 1/EAamp5e 5.$Use a 5oop that 4o3nts do7n 80 1i:e to disp5a0 a Ce5si3s)to)%ahrenheit 4on:ersion ta85e. ?%i23re 5.(@5-25%i23re 5.(isp5a0in2 a Ce5si3s)to)%ahrenheit Con:ersion5-265.5 Conditional Loops/Not 8e a85e to determine the eAa4t n3m8er o1 5oop repetitions 8e1ore 5oop eAe43tion 8e2ins-EA. /Print an initia5 promptin2 messa2e/Get the n3m8er o1 o8ser:ed :a53es/"hi5e the n3m8er o1 :a53es is ne2ati:ePrint a 7arnin2 and another promptin2 messa2eGet the n3m8er o1 o8ser:ed :a53es 5-27.#ample 5.5(Figure 5./)/The pro2ram is desi2ned to assist in monitorin2 the 2aso5ine s3pp50 in a stora2e tan= at the S3per Oi5 Compan0 re1iner0./The pro2ram is to a5ert the s3per:isor 7hen the s3pp50 o1 2aso5ine in the tan= 1a55s 8e5o7 1.O o1 the tan=Ms (.