Looping(1) For … to … do...
Can you do this? Write a program to display:
Program E1; begin writeln(1); writeln(2); writeln(3); writeln(4); writeln(5); writeln(6); end. Done!
What about this? Please enter a number: Write a program to display: Please enter a number:
Looping Program E1; var j, k:integer; begin write(‘Please enter a number’:); readln(n); for j:=1 to n do writeln(j); end.
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1 2
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1 2
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:
How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number: j>n
How does it work? Program E1; var j, k:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:
FOR … TO … DO… for I:=1 to 5 do writeln(I); writeln(1); writeln(2); writeln(3); writeln(4); writeln(5);
FOR … DOWNTO … DO… for I:=5 downto 1 do writeln(I); writeln(5); writeln(4); writeln(3); writeln(2); writeln(1);
For with multiple statements Program E1; begin for j:=1 to 3 do begin writeln(j); writeln(‘-’); end; end
Exercise 1 Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=n to 6 do writeln(j*2); end. Write down the output of the following program if 3 is the input.
Exercise 2 Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=n downto 4 do if (j mod 2)=0 then writeln(j div 2) else writeln(j*2); end. Write down the output of the following program if 6 is the input.
Exercise 3 Write programs to display: Enter a number:5 Enter a number: Enter a number:5 2,4,6,8,10, Enter a number:5 1,3,5,7,9 Enter a number:5 Enter a number:16 6,8,10,12,14,16,
Answer Q3 Program E1; var a, b, j:integer; begin write(‘Enter a number:’); readln(a); write(‘Enter a number:’; readln(b); for j:=a to b do writeln(j); end.
Answer Q3 Program E1; var a, j:integer; begin write(‘Enter a number:’); readln(a); for j:=1 to a do write(j*2, ’,’); end.
Answer Q3 Program E1; var a, j:integer; begin write(‘Enter a number:’); readln(a); for j:=1 to a-1 do write(j*2-1, ’,’); writeln(a*2-1); end.
Answer Q3 Program E1; var a, b, j:integer; begin write(‘Enter a number:’); readln(a); write(‘Enter a number:’; readln(b); for j:=a to b do if (j mod 2)=0 then write(j, ’,’); end.