Download presentation
Presentation is loading. Please wait.
Published byBrendan Wells Modified over 8 years ago
2
Looping(1) For … to … do...
3
Can you do this? 123456123456 Write a program to display:
4
Program E1; begin writeln(1); writeln(2); writeln(3); writeln(4); writeln(5); writeln(6); end. Done!
5
What about this? Please enter a number:6 1 2 3 4 5 6 Write a program to display: Please enter a number:4 1 2 3 4
6
Looping Program E1; var j, k:integer; begin write(‘Please enter a number’:); readln(n); for j:=1 to n do writeln(j); end.
7
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:
8
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
9
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
10
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
11
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
12
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
13
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
14
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 3
15
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 3 j>n
16
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:3 1 2 3
17
FOR … TO … DO… for I:=1 to 5 do writeln(I); writeln(1); writeln(2); writeln(3); writeln(4); writeln(5);
18
FOR … DOWNTO … DO… for I:=5 downto 1 do writeln(I); writeln(5); writeln(4); writeln(3); writeln(2); writeln(1);
19
For with multiple statements Program E1; begin for j:=1 to 3 do begin writeln(j); writeln(‘-’); end; end. 1-2-3-1-2-3-
20
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.
21
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.
22
Exercise 3 Write programs to display: Enter a number:5 Enter a number:10 5 6 7 8 9 10 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,
23
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.
24
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.
25
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.
26
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.