1 / 23

Lecture #5: Repeat & For

Lecture #5: Repeat & For. Anan Phonphoem Anan@cpe.ku.ac.th July 10, 2001. Iteration. Loop Execute a set of command repeatedly. Iteration. Repeat-until Loop. For Loop. While Loop. While Loop. Count Controlled. Event Controlled. Exact number of loops Exit loop after “ n ” times.

Download Presentation

Lecture #5: Repeat & For

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #5: Repeat & For Anan Phonphoem Anan@cpe.ku.ac.th July 10, 2001

  2. Iteration • Loop • Execute a set of command repeatedly

  3. Iteration Repeat-until Loop For Loop While Loop

  4. While Loop Count Controlled Event Controlled • Exact number of loops • Exit loop after “n” times • Vary number of loops • Exit loop when • condition = “False”

  5. condition False True Statement While Loop While condition do statement

  6. Repeat-Until Loop Count Controlled Event Controlled • Exact number of loops • Exit loop after “n” times • Vary number of loops • Exit loop when • condition = “False”

  7. Statement Statement Statement False condition True Repeat-until Loop Repeat Statement; … Statement; Until condition

  8. n <= 5 False True Writeln(n) n := n +1 While Loop Count Controlled While n <= 5 do begin writeln(n); n := n +1; end;

  9. n : n +1 Writeln(n) Repeat-until Loop Count Controlled Repeat writeln(n); n := n +1; Until n > 5; False n > 5 True

  10. False Rain := True True Readln(n) False n > 0 True writeln(n) Rain := False While LoopEvent Controlled rain := True; While rain do begin readln (n); if n < 0 then writeln(n) else rain := False end;

  11. Repeat-Until LoopEvent Controlled rain := True; Repeat readln (n); if n < 0 then writeln(n) else rain := False Until (rain = False); Readln(n) False n < 0 True writeln(n) Rain := False False Rain := False True

  12. For Loop • Same as While loop For variable:= initial value to final value do statement For variable:= initial value downto final value do statement

  13. For Loop for n := 1 to 5 do writeln(n); for n := 5 downto 1do writeln(n);

  14. Nested For-Loops for x := 1 to 3 do for y := 20 to 25 do writeln(x:5, y:5);

  15. Start Read “n” False n > 0 Write “fac” True fac := fac * n End n := n -1 Example 1Calculate the “n!” (n-factorial)

  16. n! program begin fac := 1; write(‘Please input n ’); readln(n); while n > 0 do begin fac := fac * n; n := n –1; end; writeln(n, ‘! = ‘, fac) end.

  17. n! program Repeat-Until begin fac := 1; write(‘Please input n ’); readln(n); Repeat begin fac := fac * n; n := n –1; end; Until (n <= 0); writeln(n, ‘! = ‘, fac) end.

  18. n! program For Begin fac := 1; write(‘Please input n ’); readln(n); For i := n downto 1 do fac := fac * i; writeln(n, ‘! = ‘, fac) end.

  19. Example 2Convert Celcius to Farenheit Celcius Farenheit 0.0 32.0 1.0 33.8 2.0 35.6 …… …… 99.0 210.2 100.0 212.0 Farenheit = Celcius * (9/5) + 32

  20. Start Write heading False cel <=100 True Far := Cel * (9/ 5) + 32 Write output End Cel := Cel +1 Example 2 Convert Celcius to Farenheit

  21. Convert Celcius to Farenheit program begin Writeln(‘Celcius’:10, ‘Farenheit’:15); Cel := 0; while Cel <= 100 do begin Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1); Cel := Cel + 1 end end.

  22. Convert Celcius to Farenheit program – Repeat-Until begin Writeln(‘Celcius’:10, ‘Farenheit’:15); Cel := 0; repeat begin Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1); Cel := Cel + 1 end until Cel > 100; end.

  23. Convert Celcius to Farenheit program -- for begin Writeln(‘Celcius’:10, ‘Farenheit’:15); for Cel :=0 to 100 do begin Far := Cel * (9/ 5) + 32; writeln(cel:10:1, farenheit:15:1) end end.

More Related