Control flow statements allow your program to make decisions and execute blocks of code repeatedly, based on specific conditions.
1. Conditional Statements
Conditional statements determine which code block is executed based on the evaluation of a Boolean expression (True or False).
A. The if Statement
The if statement is used to execute a block of code only if a condition is met.
var
Age: Integer;
begin
Age := 25;
if Age >= 18 then
begin // If block contains more than one statement, 'begin/end' is required
ShowMessage('You are an adult.');
end
else
ShowMessage('You are a minor.');
end;
Single Statement Rule: If the
thenorelseclause contains only a single statement, thebeginandendkeywords are optional. Usingbegin/endis often recommended for readability.
B. The if...then...else Statement
Used to define two distinct paths of execution: one if the condition is True, and another if it is False.
var
IsMember: Boolean;
begin
IsMember := True;
if IsMember then
TotalCost := 50.00 // No semicolon before 'else'
else
TotalCost := 75.00;
end;
C. Nested if and else if
You can chain multiple conditions using nested if statements or the cleaner else if structure.
var
Score: Integer;
begin
Score := 85;
if Score >= 90 then
Grade := 'A'
else if Score >= 80 then // Note: Object Pascal uses 'else if' or 'else begin if'
Grade := 'B'
else
Grade := 'C';
end;
D. The case Statement
The case statement provides a cleaner alternative to multiple if...else if structures when you are testing a single variable against multiple, specific constant values.
var
Day: Integer;
begin
Day := 3;
case Day of
1: DayName := 'Monday';
2, 3, 4: DayName := 'Weekday'; // Handles multiple values
5: DayName := 'Friday';
6..7: DayName := 'Weekend'; // Handles a range of values
else // Optional block for values not matched
DayName := 'Invalid Day';
end; // The 'end' closes the 'case' block
end;
2. Iteration (Loops)
Loops are used to execute a block of code repeatedly until a specific condition is met or a set number of times.
A. The for Loop (Fixed Iteration)
The for loop is used when you know exactly how many times you need to execute the loop.
- Counting Up (Ascending): Uses the keyword
to. - Counting Down (Descending): Uses the keyword
downto. - Loop Variable: The loop control variable (e.g.,
ibelow) must be declared, usually locally in the procedure’svarblock.
procedure TForm1.CountUp;
var
i: Integer;
Result: Integer;
begin
Result := 0;
// Counts i from 1 to 10
for i := 1 to 10 do
begin
Result := Result + i;
end; // end of the loop block
ShowMessage('Total: ' + IntToStr(Result));
end;
B. The while Loop (Pre-test)
The while loop checks the condition before executing the loop body. If the condition is initially False, the loop body never runs.
var
i: Integer;
begin
i := 0;
while i < 5 do
begin
i := i + 1; // Must include logic to eventually make the condition False
Memo1.Lines.Add(IntToStr(i));
end;
end;
C. The repeat...until Loop (Post-test)
The repeat...until loop executes the loop body at least once before checking the condition. The loop continues until the condition becomes True.
var
i: Integer;
begin
i := 10;
repeat
ShowMessage('Current count: ' + IntToStr(i));
i := i + 1;
until i > 10; // Loop executes once (since i=10 is not > 10), then terminates.
end;
