lecture #5: repeat & for

23
http://www.cpe.ku.ac.th/~ anan 1 Lecture #5: Repeat & For

Upload: shing

Post on 05-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Lecture #5: Repeat & For. 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. Vary number of loops Exit loop when - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

1

Lecture #5: Repeat & For

Page 2: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

2

Iteration Loop Execute a set of command repeatedly

Page 3: Lecture #5: Repeat & For

Iteration

While LoopRepeat-until Loop For Loop

Page 4: Lecture #5: Repeat & For

While Loop

Count Controlled Event Controlled

• Exact number of loops• Exit loop after “n” times

• Vary number of loops• Exit loop when condition = “False”

Page 5: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

5

While Loop

While condition do statement

conditionFalse

True

Statement

Page 6: Lecture #5: Repeat & For

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”

Page 7: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

7

Repeat-until Loop

Repeat Statement; … Statement;Until condition condition

False

True

Statement

Statement

Statement

Page 8: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

8

While Loop Count Controlled

While n <= 5 do begin writeln(n); n := n +1; end;

n <= 5False

True

Writeln(n)

n := n +1

Page 9: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

9

Repeat-until Loop Count Controlled

Repeat writeln(n); n := n +1;Until n > 5;

n > 5False

True

n : n +1

Writeln(n)

Page 10: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

10

While LoopEvent Controlledrain := True;While rain dobegin readln (n);

if n < 0 then writeln(n)

else rain := Falseend;

Rain := True False

TrueReadln(n)

n > 0

Truewriteln(n)

False

Rain := False

Page 11: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

11

Repeat-Until LoopEvent Controlled

rain := True;Repeat readln (n);

if n < 0 then writeln(n)

else rain := FalseUntil (rain = False);

Rain := False

False

True

Readln(n)

n < 0

Truewriteln(n)

False

Rain := False

Page 12: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

12

For Loop Same as While loop

For variable:= initial value to final value dostatement

For variable:= initial value downto final value do

statement

Page 13: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

13

For Loop

for n := 1 to 5 do writeln(n);

for n := 5 downto 1 do writeln(n);

Page 14: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

14

Nested For-Loops

for x := 1 to 3 dofor y := 20 to 25 do

writeln(x:5, y:5);

Page 15: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

15

Example 1Calculate the “n!” (n-factorial)

Start

Read “n”

n := n -1

n > 0 Write “fac”

End

False

True

fac := fac * n

Page 16: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

16

n! programbegin

fac := 1;write(‘Please input n ’);

readln(n);while n > 0 do

beginfac := fac * n;n := n –1;

end;writeln(n, ‘! = ‘, fac)

end.

Page 17: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

17

n! program Repeat-Untilbegin

fac := 1;write(‘Please input n ’);

readln(n);Repeat

beginfac := fac * n;n := n –1;

end;Until (n <= 0);writeln(n, ‘! = ‘, fac)

end.

Page 18: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

18

n! program ForBegin

fac := 1;write(‘Please input n ’);

readln(n);For i := n downto 1 do

fac := fac * i;writeln(n, ‘! = ‘, fac)

end.

Page 19: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

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

Page 20: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

20

Example 2 Convert Celcius to Farenheit

Start

Write heading

cel <=100

Write output

End

False

True

Far := Cel * (9/ 5) + 32

Cel := Cel +1

Page 21: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

21

Convert Celcius to Farenheit programbegin

Writeln(‘Celcius’:10, ‘Farenheit’:15);Cel := 0;while Cel <= 100 do

beginFar := Cel * (9/ 5) + 32;

writeln(cel:10:1, farenheit:15:1);Cel := Cel + 1

endend.

Page 22: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

22

Convert Celcius to Farenheit program – Repeat-Untilbegin

Writeln(‘Celcius’:10, ‘Farenheit’:15);Cel := 0;repeat

beginFar := Cel * (9/ 5) + 32;

writeln(cel:10:1, farenheit:15:1);Cel := Cel + 1

end until Cel > 100;

end.

Page 23: Lecture #5: Repeat & For

http://www.cpe.ku.ac.th/~anan

23

Convert Celcius to Farenheit program -- forbegin

Writeln(‘Celcius’:10, ‘Farenheit’:15);for Cel :=0 to 100 do

beginFar := Cel * (9/ 5) + 32;

writeln(cel:10:1, farenheit:15:1)end

end.