LOOPS
The most common loop in Pascal is the FOR loop. The statement
inside the for block is executed a number of times depending on
the control condition. The format's for the FOR command is,
FOR var_name := initial_value TO final_value DO program_statement; FOR var_name := initial_value TO final_value DO begin program_statement; {to execute more than one statement in a for } program_statement; {loop, you group them using the begin and } program_statement {end statements } end; {semi-colon here depends upon next keyword } FOR var_name := initial_value DOWNTO final_value DO program_statement;
You must not change the value of the control variable (var_name) inside the loop. The following program illustrates the for statement.
program CELSIUS_TABLE ( output ); var celsius : integer; fahrenheit : real; begin writeln('Degree''s Celsius Degree''s Fahrenheit'); for celsius := 1 to 20 do begin fahrenheit := ( 9 / 5 ) * celsius + 32; writeln( celsius:8, ' ',fahrenheit:16:2 ) end end.
SELF TEST 11
What is the resultant output when this program is run.
program FOR_TEST ( output ); var s, j, k, i, l : integer; begin s := 0; for j:= 1 to 5 do begin write( j ); s := s + j end; writeln( s ); for k := 0 to 1 do write( k ); for i := 10 downto 1 do writeln( i ); j := 3; k := 8; l := 2; for i := j to k do writeln( i + l ) end. Click here for answer
PROGRAM NINE
For the first twenty values (1-20) of fahrenheit, print out the
equivalent degree in celsius (Use a tabular format, with
appropiate headings). [C = ( 5 / 9 ) * (Farenhiet - 32)]
Use the statement writeln('<14>'); to clear the screen. In Turbo Pascal for DOS, the statement clrscr; can be used to clear the screen.