2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
2 nd Semester Last week … Review… Flowchart Represent Looping While…DO Syntax While…DO Template Count Control Event Control
2 nd Semester 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)
2 nd Semester Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
2 nd Semester Flowchart 1: While…DO in Flowchart View Condition Statement1; Statement2; Statement3; Out False True In WHILE…DO
2 nd Semester While…DO Categories While Loop Count ControlledEvent Controlled Special Case Special Case: For…DO Increase/Decrease by 1
2 nd Semester 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
2 nd Semester 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.
2 nd Semester 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.
2 nd Semester Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
2 nd Semester 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;
2 nd Semester 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.
2 nd Semester For Example Write into the screen fori := 1todo for i := 1 to 5 do write( i, ‘ ‘ ); Write into the screen fori := 5downto1do for i := 5 downto 1 do write( i, ‘ ‘ );
2 nd Semester Nested For Loop fortodo for x := 1 to 3 do fortodo for y := 20 to 25 do writeln(x:5, y:5);
2 nd Semester Nested For Loop fortodo for i:=1 to n dobegin for j:=1 to i do begin write( j, ' ' ); end; writeln;end;
2 nd Semester Outline WHILE…DO VS FOR...DO For…TO…DO Syntax Example
2 nd Semester Example: Convert Celcius to Farenheit Celcius Farenheit … … Farenheit = Celcius * (9/5) + 32
2 nd Semester 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.
2 nd Semester 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.
2 nd Semester Conclusions Iteration While Loop Repeat-until LoopFor Loop
2 nd Semester 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;
2 nd Semester What will we learn in Next Class? Looping Techniques REPEAT…UNTIL