Download presentation
Presentation is loading. Please wait.
Published byTracy Mervin Fitzgerald Modified over 8 years ago
1
2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
2 nd Semester 2004 2 Last week … Review… Flowchart Represent Looping While…DO Syntax While…DO Template Count Control Event Control
3
2 nd Semester 2004 3 Summary Before Today!!! We learned how to program in this way Sequential Programming Selection Programming (IF/Case) Repetition Programming (Looping, Iteration) Repetition Programming (Looping, Iteration)
4
2 nd Semester 2004 4 Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
5
2 nd Semester 2004 5 Flowchart 1: While…DO in Flowchart View Condition Statement1; Statement2; Statement3; Out False True In WHILE…DO
6
2 nd Semester 2004 6 While…DO Categories While Loop Count ControlledEvent Controlled Special Case Special Case: For…DO Increase/Decrease by 1
7
2 nd Semester 2004 7 Count Controlled Loop Example n! flowchart Start End read(n) fac:=n; Tot:=1; fac>0 Tot:=Tot*fac; fac:=fac-1; Write(Tot); TRUE FALSE
8
2 nd Semester 2004 8 n! program: WHILE…DO Program N_FACTORIAL(input, output); VAR total, fac, n : integer; begin write(’Please input n ’); readln(n); Fac:=n; Total:=1; {WHILE N! Calculate} {WHILE N! Calculate} while (fac > 0) do begin total := fac * total; fac:=fac-1;end; writeln(n, ’! = ’, total) end.
9
2 nd Semester 2004 9 n! program: FOR…DO Program N_FACTORIAL(input, output); VAR total, fac, n : integer; begin write(’Please input n ’); readln(n); TOTAL:=1; {FOR N! Calculate} {FOR N! Calculate} FOR FAC:=N downto 1 do begin total := fac * total; end; writeln(n, ’! = ’, total) end.
10
2 nd Semester 2004 10 Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
11
2 nd Semester 2004 11 For Loop count-controlled Special statement for writing count-controlled loops. forcounter := initialtofinaldo for counter := initial to final dobegin …end; counter := initial; while (counter <= final) do begin… counter := counter + 1; end;
12
2 nd Semester 2004 12 For Loop Syntax for todo for counter :=initial to final do statement; fordowntodo for counter:=initial downto final do statement; Increase by 1 Decrease by 1 The value of counter must not be modified in statement. #note The value of counter must not be modified in statement.
13
2 nd Semester 2004 13 For Example Write 1 2 3 4 5 into the screen fori := 1todo for i := 1 to 5 do write( i, ‘ ‘ ); Write 5 4 3 2 1 into the screen fori := 5downto1do for i := 5 downto 1 do write( i, ‘ ‘ );
14
2 nd Semester 2004 14 Nested For Loop fortodo for x := 1 to 3 do fortodo for y := 20 to 25 do writeln(x:5, y:5);
15
2 nd Semester 2004 15 Nested For Loop fortodo for i:=1 to n dobegin for j:=1 to i do begin write( j, ' ' ); end; writeln;end;
16
2 nd Semester 2004 16 Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
17
2 nd Semester 2004 17 Example: Convert Celcius to Farenheit Celcius Farenheit 0 32.0 1 33.8 2 35.6 … … 99 210.2 100 212.0 Farenheit = Celcius * (9/5) + 32
18
2 nd Semester 2004 18 WHILE…DO: Convert Celcius to Farenheit Program Celsius_to_Fahrenheit(input, output); Var Far : Real; Cel : Integer; begin Writeln(’Celsius’:10, ’Fahrenheit’:15); {While Loop Begin Calculate Celsius to Fahrenheit} Cel := 0; {While Loop Begin Calculate Celsius to Fahrenheit} while (Cel <= 100) do begin Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1); Cel := Cel + 1; end; end.
19
2 nd Semester 2004 19 FOR...DO: Convert Celcius to Farenheit Program Celsius_to_Fahrenheit(input, output); Var Far : Real; Cel : Integer; begin Writeln(’Celsius’:10, ’Fahrenheit’:15); {FOR Loop Begin Calculate Celsius to Fahrenheit} FOR CEL:=0 TO 100 DO begin Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1); end; end.
20
2 nd Semester 2004 20 Conclusions Iteration While Loop Repeat-until LoopFor Loop
21
2 nd Semester 2004 21 Conclusions For loop Syntax Used only in Count Controlled Style for todo for counter := initial to final do statement; fordowntodo for counter:=initial downto final do statement;
22
2 nd Semester 2004 22 What will we learn in Next Class? Looping Techniques REPEAT…UNTIL
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.