Looping 4 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Looping 4

Description:

Looping (4) A loop within a loop. Nested FOR loops. for I:=1 to 3 do. for ... write( Enter size of an isosceles triangle: '); readln(a); for i:=1 to a do. begin ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 31
Provided by: peter67
Category:

less

Transcript and Presenter's Notes

Title: Looping 4


1
Looping (4)
  • A loop within a loop

2
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 0
3
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 1
4
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 1
11
5
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 2
12
6
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 1 J 3
13
7
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 3
8
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 1
9
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 1
21
10
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 2
22
11
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 2 J 3
23
12
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 3
13
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 1
14
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 1
31
15
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 2
32
16
Nested FOR loops
Example
for I1 to 3 do for J1 to 3 do
writeln(I,,J)
I 3 J 3
33
17
Example 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.
18
Output
1 x 1 1 1 x 2 2 2 x 1 2 2 x 2 4 3 x 1
3 3 x 2 6
19
Example 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.
20
Output
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.
21
ExerciseWrite 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)
22
ExerciseWrite 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)
23
Answer 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.
24
Answer 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.
25
Answer 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.
26
Answer 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.
27
Answer 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.
28
Answer 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.
29
Answer 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.
30
Answer 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.
Write a Comment
User Comments (0)
About PowerShow.com