Conditions
IF Statements
If
statements are concerned with Boolean logic. If the statement is true, the block of code associated with theif statement
is executed. If the statement is false, control either jumps to the line after theif statement
, or after theend
keyword of anif statement
block.
var numb:= 1; if numb = 1 then begin // statements that will execute if the value of the numb variable is 1, // will be placed here. end;
- You can remove the
begin
andend
keywords if your statement to execute is a single line statement. PascalAbc understands that if nobegin
andend
are used, the line immediately after the if (condition) will be executed if the condition is true.
Else clauses
IF statements
can also have associatedelse
clauses. Theelse
clause executes when theif
statement is false:
var numb:= 1; if numb = 1 then begin // Block of code executes if the value of the numb variable is 1. end else begin // Block of code executes if the value of the numb variable is not 1. end
Else if clauses (or Chained If statements)
If
statements can also have associatedelse if
clauses. The clauses are tested in the order that they appear in the code after theif
statement. If any of the clauses returns true, the block of code associated with that statement is executed and control leaves the block of code associated with the entireif
construct.
var numb:= 1; if numb = 1 then begin // Block of code executes if the value of the numb variable is 1. end else if numb = 0 then begin // Block of code executes if the value of the numb variable is 0. end else begin // Block of code executes if the value of the numb variable is neither above answers. end
- The semicolon before
else
is not needed!
- You can create as many
else if
blocks as necessary.
To do: A number of season (Winter is the first) is given. Output a name of the entered season number. It is better to use the chained If statements.
⤵ Algorithm:

Logical (boolean) operations
- We can define variables of boolean type:
var A,B: boolean; A := True; B := False; Print(A and B); Print(A or B); Print(not A);
A and B
is True ifA
is True andB
is True at the same time. In other casesA and B
is False
A or B
is False ifA
is False andB
is False. In other casesA or B
is True
not A
has opposite value:not A
is True ifA
is False
if
statements:
- Conditions may consist of logic operations:
not
,or
,and
. If there are more than one condition, so each condition must be surrounded by round brackets.
if (year < 20) or (year > 18) then begin // if body endIf one of the conditions or both conditions are True then
if body
executes.- Or another example:
var a: = 5; if (not (a<4)) and (7>5) then // ← True begin // if body end
- The statements
write
andprint
can also return True or False:
//... a = 5; write (a >= 5); // returns True // ...
The «most popular» errors:
- You don’t need to write semicolon before
else
clause.
- Don’t forget about
begin
..end
in the cases when there is more than one lines of code afterthen
orelse
.
Case statement
If there are too many else if
statements, code can become difficult to understand. In this case, a better solution is to use a switch
statement:
var numb:= 1; case numb of 1,2 : writeln ('1 or 2'); // Block of code executes if the value of numb is 1 or 2. 3: writeln('3'); // Block of code executes if the value of numb is 2. 4: writeln('4'); // Block of code executes if the value of numb is 3. 5: writeln('5'); // Block of code executes if the value of numb is 5. else writeln('nothing matches'); // Block of code executes if nothing matches. end
- A block labeled
else
will execute when none of the other blocks match.
Using String type
Case statement can check the variable of String type.
Lab 1 Example:
Given: the english words ‘dog’, ‘use’, ‘find’.
To do: Output translations of the words into russian.
Given: the english words ‘dog’, ‘use’, ‘find’.
To do: Output translations of the words into russian.
⤵ Algorithm:

Using ranges and enumerations
Lab 2 Example:
Given: Month ordinal.
To do: Output corresponding name of a season.
Given: Month ordinal.
To do: Output corresponding name of a season.
⤵ Algorithm:
