0% found this document useful (0 votes)
83 views

Pascal - Exercise Example

The document provides examples of different loop constructs in the Pascal programming language including while loops, for loops, and repeat-until loops. It shows the syntax and output of sample programs using each loop type.

Uploaded by

dinathmethyuga8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Pascal - Exercise Example

The document provides examples of different loop constructs in the Pascal programming language including while loops, for loops, and repeat-until loops. It shows the syntax and output of sample programs using each loop type.

Uploaded by

dinathmethyuga8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Knowledge is Strength Knowledge is Strength Knowledge is Strength Knowledge is Strength Knowledge is

Strength
General Certificate of Education (Ordinary Level) Examination (O/L) kj ks¾foaYh / New Syllabus
Pascal program – Grade 11 Priyanthi Fernando 80 S I,II

Example: 1 WHILE DO Loop


Example: 3 Repeat-Until Loop
program whileLoop; program repeatUntilLoop;
var var
a: integer; a: integer;

begin
begin a := 10;
a := 10; (* repeat until loop execution *)
while a < 20 do repeat
begin writeln('value of a: ', a);
writeln('value of a: ', a); a := a + 1
a := a + 1; until a = 20;
end.
end;
readln; Result
end. value of a: 10
value of a: 11
Example: 2 FOR DO Loop value of a: 12
value of a: 13
The syntax for the for-do loop in Pascal is as value of a: 14
value of a: 15
follows – value of a: 16
value of a: 17
for < variable-name > := < initial_value > to value of a: 18
value of a: 19
[down to] < final_value > do S;

Example: 4 Repeat-Until Loop


program repeatUntilLoop;
for i:= 1 to 10 do writeln(i); var
a: integer;

begin
program forDOLoop;
var a := 10;
a: integer; (* repeat until loop execution *)
repeat
begin writeln('value of a: ', a);
for a := 10 to 20 do a := a + 1
until a > 20;
begin
readln;
writeln('value of a: ', a);
end; end.
readln;
end.
Result
Result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
Example 5. Example 7: Hello program

Accessing Array Elements program whileLoop;


program exArrays; var
var count: integer;
n: array [1..10] of integer; begin
i, j: integer;
for count := 0 to 5 do
begin write('Hello');
for i := 1 to 10 do readln;
n[ i ] := i + 100; end.
for j:= 1 to 10 do
writeln('Element[', j, '] = ',
n[j] );
end.
Result:

Example 8:

program whileLoop;
var
count:integer;
begin
Example 6.
for count := 0 to 5 do
program whileLoop; write('Hello, ');
var readln;
x: integer; end.
count:integer;
begin
for x := 0 to 2 do
write('#');
For x := 1 to 3 do
writeln('#');
readln;
end.

Example 9.

program whileLoop;
var
revision count:integer;
begin
for count := 0 to 5 do
write('Hello ');
readln;
end. *
program whileLoop; *
var
x: integer; *
count:integer; *
begin
for x := 0 to 2 do
*
write('#');
For x := 1 to 3 do
writeln('#');
readln;
end.

program forDOLoop;
var
i: integer;

begin
for i := 1 to 5 do

begin
writeln('*');
end;
readln;
end.

Answer

You might also like