Title: Looping 4
1Looping (4)
2Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 0
3Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 1
4Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 1
11
5Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 2
12
6Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 3
13
7Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 3
8Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 1
9Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 1
21
10Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 2
22
11Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 3
23
12Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 3
13Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 1
14Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 1
31
15Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 2
32
16Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 3
33
17Example 2
PROGRAM MultiplicationTable(INPUT, OUTPUT) VAR
Num1, Num2 integer BEGIN Num1 1 Num2
1 WHILE Num1 lt 3 DO BEGIN WHILE
Num2 lt 2 DO BEGIN
WRITELN(Num1, ' x ', Num2, ' ', Num1Num2)
Num2 Num2 1 END Num2
1 Num1 Num1 1 WRITELN
END END.
18Output
1 x 1 1 1 x 2 2 2 x 1 2 2 x 2 4 3 x 1
3 3 x 2 6
19Example 3
PROGRAM BankingTransaction(INPUT, OUTPUT) VAR
Choice integer BEGIN REPEAT
WRITELN('Automatic Teller Machine Menu ')
WRITELN('1. Check Account Balance.')
WRITELN('2. Cash withdrawal. ') WRITELN('3.
Exit.') WRITELN REPEAT
WRITE('Please enter your choice (1 - 3) ')
READLN(Choice) IF (Choice lt 1) OR
(Choice gt 3) THEN WRITELN('Invalid
entry Please enter again.') UNTIL (Choice
gt 1) AND (Choice lt 3) IF Choice 1
THEN WRITELN('Your selection is check
account balance.') ELSE IF Choice 2 THEN
WRITELN('Your selection is cash
withdrawal.') ELSE
WRITELN('End of transaction.') WRITELN
UNTIL (Choice 3) END.
20Output
Automatic Teller Machine Menu 1. Check Account
Balance. 2. Cash withdrawal. 3. Exit. Please
enter your choice (1 - 3) 1 Your selection is
check account balance. Automatic Teller Machine
Menu 1. Check Account Balance. 2. Cash
withdrawal. 3. Exit. Please enter your choice (1
- 3) 2 Your selection is cash withdrawal. Automa
tic Teller Machine Menu 1. Check Account
Balance. 2. Cash withdrawal. 3. Exit. Please
enter your choice (1 - 3) 4 Invalid entry
Please enter again. Please enter your choice (1 -
3) 3 End of transaction.
21ExerciseWrite programs to display the following
Enter a number4
Enter a number5 Enter a number4
(1)
(2)
Enter a number4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Enter a lettere a a a a a b b b b b c c c c c d
d d d d e e e e e
(3)
(4)
22ExerciseWrite programs to display the following
Enter a number4 1 1 2 1 2 3 1 2 3 4
Enter a number4 4 3 4 2 3 4 1 2 3 4
(5)
(6)
Enter a number4 4 3 2 1 3 2 1 2 1 1
Page 152 Question 10
(7)
(8)
23Answer Q1
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i1
to a do begin for j1 to a do
write( ) writeln end end.
24Answer Q2
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a)
write(Enter a number) readln(b) for i1
to b do begin for j1 to a do
write( ) writeln end end.
25Answer Q3
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i1
to a do begin for j1 to a do
write(j, ) writeln end end.
26Answer Q4
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i1
to a do begin for j1 to a do
write(i, ) writeln end end.
27Answer Q5
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i1
to a do begin for j1 to i do
write(j, ) writeln end end.
28Answer Q6
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i1
to a do begin for j5-i to 4 do
write(j, ) writeln end end.
29Answer Q7
Program E1 var a, b, i, jinteger begin
write(Enter a number) readln(a) for i4
downto 1 do begin for ji downto 1 do
write() writeln end end.
30Answer Q8
Program E1 var a, b, i, jinteger begin
write(Enter size of an isosceles triangle )
readln(a) for i1 to a do begin for
ji to a-1 do write( ) for j1
to i2-1 do write() writeln
end end.