2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO...
-
Upload
tracy-mervin-fitzgerald -
Category
Documents
-
view
218 -
download
0
Transcript of 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO...
12nd Semester 2004
204111Module5-1
Looping TechniqueLooping TechniqueFOR…DOFOR…DO
อภิ�รั�กษ์� จั�นทรั�สรั�างAphirak Jansang
[email protected]://www.cpe.ku.ac.th/~aphirak
Computer Engineering DepartmentKasetsart University, Bangkok THAILAND
22nd Semester 2004
Last week … Review… Flowchart Represent Looping While…DO Syntax While…DO Template
Count Control Event Control
32nd Semester 2004
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)
42nd Semester 2004
Outline WHILE…DO VS FOR...DO For…TO…DO
Syntax Example
52nd Semester 2004
Flowchart 1: While…DO in Flowchart View
Condition
Statement1;
Statement2;
Statement3;
OutOut
FalseFalse
TrueTrue
InInWHILE…DOWHILE…DO
62nd Semester 2004
While…DO Categories
While Loop
Count Controlled Event Controlled
Special CaseSpecial Case: For…DOIncrease/Decrease by 1
72nd Semester 2004
Count Controlled Loop Examplen! flowchart Start
End
read(n)
fac:=n; Tot:=1;
fac>0
Tot:=Tot*fac;
fac:=fac-1;
Write(Tot);
TRUETRUE
FALSEFALSE
82nd Semester 2004
n! program: WHILE…DOProgram 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 while (fac > 0)(fac > 0) do dobeginbegin
total := fac * total;total := fac * total;fac:=fac-1;fac:=fac-1;
end;end;writeln(n, ’! = ’, total)
end.
92nd Semester 2004
n! program: FOR…DOProgram 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 FOR FAC:=NFAC:=N downto downto 11 do dobeginbegin
total := fac * total;total := fac * total;end;end;
writeln(n, ’! = ’, total)end.
102nd Semester 2004
Outline WHILE…DO VS FOR...DO For…TO…DO
Syntax Example
112nd Semester 2004
For Loop Special statement for writing count-count-
controlledcontrolled loops.
forfor counter := counter := initialinitial toto finalfinal dodobeginbegin
…end;end;
counter := initial;while while (counter <= (counter <= finalfinal)) do dobeginbegin
……counter := counter + 1;counter := counter + 1;
end;end;
122nd Semester 2004
For Loop Syntax
for for counter :=initial toto final dodostatement;
forfor counter:=initial downtodownto final dodostatement;
Increase by 1
Decrease by 1
#note The value of counter must not be modified in statement.The value of counter must not be modified in statement.
132nd Semester 2004
For Example Write 1 2 3 4 5 into the screen
forfor i := 1i := 1 toto 5 dodo write( i, ‘ ‘ );
Write 5 4 3 2 1 into the screen
forfor i := 5i := 5 downtodownto 11 dodo write( i, ‘ ‘ );
142nd Semester 2004
Nested For Loopforfor x := 1 toto 3 dodo
forfor y := 20 toto 25 dodo writeln(x:5, y:5);
152nd Semester 2004
Nested For Loopforfor i:=1 toto n dodo
beginbegin for j:=1 to i do beginbegin write( j, ' ' ); end;end; writeln;
end;end;
162nd Semester 2004
Outline WHILE…DO VS FOR...DO For…TO…DO
Syntax Example
172nd Semester 2004
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
182nd Semester 2004
WHILE…DO:Convert Celcius to FarenheitProgram Celsius_to_Fahrenheit(input, output);Var Far : Real;
Cel : Integer;begin
Writeln(’Celsius’:10, ’Fahrenheit’:15);Cel := 0;
{While Loop Begin Calculate Celsius to Fahrenheit}{While Loop Begin Calculate Celsius to Fahrenheit}while while (Cel <= 100)(Cel <= 100) do dobeginbegin
Far := Cel * (9/ 5) + 32; Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1);writeln(cel:10, far:15:1);Cel := Cel + 1;Cel := Cel + 1;
end;end;end.
192nd Semester 2004
FOR...DO:Convert Celcius to FarenheitProgram Celsius_to_Fahrenheit(input, output);Var Far : Real;
Cel : Integer;begin
Writeln(’Celsius’:10, ’Fahrenheit’:15);
{FOR Loop Begin Calculate Celsius to Fahrenheit}{FOR Loop Begin Calculate Celsius to Fahrenheit}FOR CEL:=0 TO 100 DOFOR CEL:=0 TO 100 DObeginbegin
Far := Cel * (9/ 5) + 32; Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1);writeln(cel:10, far:15:1);
end;end;end.
202nd Semester 2004
Conclusions
Iteration
While LoopRepeat-until Loop For Loop
212nd Semester 2004
Conclusions For loop Syntax
Used only in Count Controlled Style
for for counter := initial toto final dodostatement;
forfor counter:=initial downtodownto final dodostatement;
222nd Semester 2004
What will we learn in Next Class?
Looping TechniquesLooping TechniquesREPEAT…UNTILREPEAT…UNTIL