2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO...

22
1 2 nd Semester 2004 204111 Module5-1 Looping Technique Looping Technique FOR…DO FOR…DO ออออออออ อออออออออออ Aphirak Jansang [email protected] http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

Transcript of 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO...

Page 1: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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

Page 2: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

22nd Semester 2004

Last week … Review… Flowchart Represent Looping While…DO Syntax While…DO Template

Count Control Event Control

Page 3: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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)

Page 4: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

42nd Semester 2004

Outline WHILE…DO VS FOR...DO For…TO…DO

Syntax Example

Page 5: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

52nd Semester 2004

Flowchart 1: While…DO in Flowchart View

Condition

Statement1;

Statement2;

Statement3;

OutOut

FalseFalse

TrueTrue

InInWHILE…DOWHILE…DO

Page 6: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

62nd Semester 2004

While…DO Categories

While Loop

Count Controlled Event Controlled

Special CaseSpecial Case: For…DOIncrease/Decrease by 1

Page 7: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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

Page 8: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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.

Page 9: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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.

Page 10: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

102nd Semester 2004

Outline WHILE…DO VS FOR...DO For…TO…DO

Syntax Example

Page 11: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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;

Page 12: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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.

Page 13: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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, ‘ ‘ );

Page 14: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

142nd Semester 2004

Nested For Loopforfor x := 1 toto 3 dodo

forfor y := 20 toto 25 dodo writeln(x:5, y:5);

Page 15: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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;

Page 16: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

162nd Semester 2004

Outline WHILE…DO VS FOR...DO For…TO…DO

Syntax Example

Page 17: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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

Page 18: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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.

Page 19: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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.

Page 20: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

202nd Semester 2004

Conclusions

Iteration

While LoopRepeat-until Loop For Loop

Page 21: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

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;

Page 22: 2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th

222nd Semester 2004

What will we learn in Next Class?

Looping TechniquesLooping TechniquesREPEAT…UNTILREPEAT…UNTIL