You are on page 1of 4

Programming with Pascal – Statements

Statement
A statement gives instructions to the computer that you want to carry out. A simple statement is a single
instruction while a compound statement comprises two or more statements that are enclosed by the
keywords ‘begin’ and ‘end’. A compound statement is useful if you want to perform some instructions in
sequence or repetitively before moving on to the nest set of statements.

Statement Example Explanation


Input Statement: Read and Read; Readln. The cursor stays next to the text
Readln reads data fron the entered. The cursor moves to the
keyboard, and waits for the next line after the text is entered
enter key on the keyboard to be
pressed.
Assignment Statement: Places Mark:= 30; :=is called the assignment operator.
data in a variable. The variable is on the left of the
assignment operator. The value is on
the right of the assignment operator.
The semicolon ; ends the statement,
so Mark is assigned 30.

Output Statement: Show the Write; Writeln; The cursor remains at the same place
output to the monitor screen Write(Mark); on the screen. The cursor goes to the
Writeln(Mark); start of the next line. Outputs 30.
Write('Score of',Mark); Outputs 30.
Writeln('Score of',Mark); | (|is the cursor)
Outputs score of 30|
Outputs score of 30
| (|is the cursor)

Compound Statement Begin Begin(of a compound ststement)


Mark:=Mark +; two or more consecutive ststements
Writeln(Mark); End;(of a compound statement)
End.

https://www.tutorialspoint.com
Loops

There may be a situation when you need to execute a block of code several number of times. In
general statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on. Programming languages provide various control structures
that allow for more complicated execution paths. A loop statement allows us to execute a
statement or group of statements multiple times and following is the general from of a loop
statement in most of the programming languages:

Pascal programming language provides the following types of loop constructs to handle looping
requirements. Click the following links to check their detail.

Loop Type Description


while-do loop Repeats a statement or group of
statements until a given condition is
true. It tests the condition before
executing the loop body.
for-do loop Execute a sequence of statements
multiple times and abbreviates the
code that manages the loop
variable.
repeat-until loop Like a while statement, except that
it tests the condition at the end of
the loop body.
nested loops You can use one or more loop inside

https://www.tutorialspoint.com
any another while, for or repeat
until loop.

While-do loop
A while-do loop statement in Pascal allows repetitive computations till some test condition is
satisfied. In other words it repeatedly executes a target statement as long as a given condition
is true.
Syntax:
The syntax of a while-do loop is: while (condition) do S;

Where condition is a Boolean or relational expression whose value would be true or false and S
is a simple statement or group of statements within BEGIN ... END block.
For example,

while number>0 do
begin
sum := sum + number;
number := number - 2;
end;

When the condition becomes false, program control passes to the line immediately following
the loop.

Here key point of the while loop is that the loop might not ever run. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.

https://www.tutorialspoint.com
Example:

program whileLoop;
var
a: integer;
begin
a := 10;
while a < 20 do
begin
writeln('value of a: ', a);
a := a + 1;
end;
end.

When the above code is compiled and executed, it produces following 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

Programming with Pascal Lesson 3 Homework.

Using the code provided in Lesson 3 handout, add a writeln statement below the loop to display the
value of the variable “a” saying “Your loop has ended because “a” has reached:”

5 Marks

https://www.tutorialspoint.com

You might also like