Control Flow refers to the order in which statements are executed. Conditional statements allow the program to branch its execution path, making your code dynamic and responsive.
1. The if...then Statement
The simplest conditional statement checks a single Boolean expression.
// Syntax 1: Single statement executed if condition is True
if BooleanExpression then Statement;
A. Simple Example
var
Grade: Integer;
begin
Grade := 85;
if Grade >= 70 then
Writeln('Passed the exam.'); // Executed only if Grade is 70 or higher
Writeln('Processing complete.');
end.
2. The if...then...else Statement
This structure provides two execution paths: one for when the condition is True (then block) and one for when the condition is False (else block).
// Syntax 2: Two paths: one for True, one for False
if BooleanExpression then
StatementIfTrue
else
StatementIfFalse;
Crucial Syntax Note: The statement immediately before the
elsekeyword must not have a semicolon (;). If you place a semicolon there, Pascal treats theif...thenblock as complete, leading to a syntax error when it encounters theelse.
Example with else
var
Age: Integer;
begin
Age := 15;
if Age >= 18 then
Writeln('Access Granted.')
else // Note: NO semicolon before the 'else' keyword
Writeln('Access Denied: Must be 18 or older.');
end.
3. Block Statements (begin...end)
If you need to execute multiple statements within the then or else sections, you must wrap them in a begin...end block.
if A > 100 then
begin // Start of True block
Writeln('Value is high.');
A := A - 100; // Second statement
end
else // Note: NO semicolon before 'else'
begin // Start of False block
Writeln('Value is low.');
A := A + 10;
end; // Semicolon here separates this entire block from the next statement
4. Nested and Cascaded if Statements
A. Cascaded if...else if
To check multiple, mutually exclusive conditions (e.g., assigning letter grades), you can cascade if statements using else if.
if Grade >= 90 then
Writeln('Grade A')
else if Grade >= 80 then
Writeln('Grade B')
else if Grade >= 70 then
Writeln('Grade C')
else
Writeln('Grade F');
B. Nested if
An if statement can be fully contained within another if statement.
if User.IsLoggedIn then
begin
if User.IsAdmin then // Nested check
Writeln('Welcome, Administrator.')
else
Writeln('Welcome, Standard User.');
end
else
Writeln('Please log in first.');
5. Boolean and Relational Operators
Conditional statements rely on expressions that evaluate to a Boolean (True or False).
A. Relational (Comparison) Operators
Used to compare values.
| Operator | Meaning | Example |
= | Equal to | A = B |
<> | Not equal to | A <> B |
> | Greater than | A > B |
< | Less than | A < B |
>= | Greater than or equal to | A >= B |
<= | Less than or equal to | A <= B |
B. Logical Operators
Used to combine multiple Boolean expressions.
| Operator | Meaning | Example |
and | Both conditions must be True. | if (Age >= 18) and (HasID) |
or | At least one condition must be True. | if (Day = 'Sat') or (Day = 'Sun') |
not | Inverts the Boolean result. | if not IsActive then... |
6. The case Statement
The case statement is used when you need to choose among several execution paths based on the discrete value of a single ordinal expression (e.g., an Integer, Char, or Enum). It’s cleaner than a long cascaded if...else if chain.
var
Option: Integer;
begin
Option := 2;
case Option of // Check the value of 'Option'
1: Writeln('Selected Option One.');
2, 3: // Multiple values can share the same command
Writeln('Selected Option Two or Three.');
5..10: // Range of values
Writeln('Selected Option Five through Ten.');
else // Optional: Executes if no match is found
Writeln('Invalid option.');
end; // Must end the case statement
end.
