1 / 17

Lecture #4: While … do

Lecture #4: While … do. อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th. Iteration. Loop Execute a set of command repeatedly. Iteration. Repeat-until Loop. For Loop. While Loop. condition. False. True. Statement. While Loop. While condition do statement. While Loop.

xannon
Télécharger la présentation

Lecture #4: While … do

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 #4: While…do อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

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

  3. Iteration Repeat-until Loop For Loop While Loop

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

  5. While Loop Count Controlled Event Controlled

  6. n <= 5 False True Writeln(n) n := n +1 While Loop :Count Controlled While n <= 5 do begin writeln(n); n := n +1; end; {while} n = 0;

  7. While Loop Count Controlled Event Controlled • Exact number of loops • Exit loop after “n” times • Boolean Flag • Sentinel Value

  8. False Rain := True True Readln(n) False n > 0 True writeln(n) Rain := False Event Controlled: Boolean Flag rain := True; While rain do begin readln (n); if n > 0 then writeln(n) else rain := False end; {while}

  9. Event Controlled:Sentinel Value • Read on or more data in each loop • When should we stop reading ? • Instruct user to enter a unique number called “Sentinel Value”

  10. Event Controlled:Sentinel Value Program test; Const Stop_Value = -999; Var num,sum: integer; Begin Readln(num); while num <> Stop_value do begin sum := sum + num; Readln (num); end; {while} writeln(sum); End.

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

  12. Number of loop Example: Readln (n); While n <= 5 do begin writeln(n); n := n +1; end; {while} • Exact number of loop • Vary number of loop • The least number of loop ? NONE

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

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

  15. 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

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

  17. 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 {while} end.

More Related