IF Statements

  • If statements are concerned with Boolean logic. If the statement is true, the block of code associated with the if statement is executed. If the statement is false, control either jumps to the line after the if statement, or after the end keyword of an if 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 and end keywords if your statement to execute is a single line statement. PascalAbc understands that if no begin and end are used, the line immediately after the if (condition) will be executed if the condition is true.

Else clauses

  • IF statements can also have associated else clauses. The else clause executes when the if 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 associated else if clauses. The clauses are tested in the order that they appear in the code after the if 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 entire if 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.
Sample:
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 if A is True and B is True at the same time. In other cases A and B is False
  • A or B is False if A is False and B is False. In other cases A or B is True
  • not A has opposite value: not A is True if A is False
Logical (boolean) operations in if statements:
  • Conditions may consist of logic operations: notorand. If there are more than one condition, so each condition must be surrounded by round brackets.
For example if we must check two conditions:
if (year < 20) or (year > 18) then
begin
  // if body
end
If 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 and print 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 after then or else.

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.

⤵ Algorithm:


Using ranges and enumerations

Lab 2 Example:
Given: Month ordinal.
To do: Output corresponding name of a season.

⤵ Algorithm: