Control Structures in Delphi: If, Case, and Loops

August 2, 2024

Control Structures in Delphi: If, Case, and Loops

Welcome back to our Delphi programming course! In this lesson, we will delve into control structures, which are essential for managing the flow of your Delphi applications. Control structures allow you to execute certain blocks of code based on specific conditions, making your programs dynamic and responsive.

If Statements

The If statement is one of the most fundamental control structures in Delphi. It allows you to execute a block of code only if a specified condition is true. Here’s the basic syntax:

if condition then
begin
  // code to execute if condition is true
end;

Here’s a practical example:

var
  age: Integer;
begin
  age := 20;
  if age >= 18 then
  begin
    WriteLn('You are an adult.');
  end;
end;

In this example, if the variable age is greater than or equal to 18, the message ‘You are an adult.’ will be printed.

If-Else Statements

To add more complexity, you can use If-Else statements. This allows you to define an alternative block of code that executes if the condition is false:

if condition then
begin
  // code if condition is true
end
else
begin
  // code if condition is false
end;

Here’s an example:

var
  age: Integer;
begin
  age := 16;
  if age >= 18 then
  begin
    WriteLn('You are an adult.');
  end
  else
  begin
    WriteLn('You are not an adult yet.');
  end;
end;

Case Statements

The Case statement is another control structure that allows you to execute different blocks of code based on the value of a variable. This is particularly useful when you have multiple conditions to check:

case variable of
  value1: begin
    // code for value1
  end;
  value2: begin
    // code for value2
  end;
  else
    // code if none match
end;

Here’s a practical example:

var
  day: Integer;
begin
  day := 3;
  case day of
    1: WriteLn('Monday');
    2: WriteLn('Tuesday');
    3: WriteLn('Wednesday');
    4: WriteLn('Thursday');
    5: WriteLn('Friday');
    6: WriteLn('Saturday');
    7: WriteLn('Sunday');
    else WriteLn('Invalid day');
  end;
end;

In this example, if the variable day is 3, the program will output ‘Wednesday’.

Loops

Loops are control structures that allow you to execute a block of code multiple times. Delphi supports several types of loops, including For loops, While loops, and Repeat-Until loops.

For Loops

The For loop is used when you know in advance how many times you want to execute a block of code:

for variable := start_value to end_value do
begin
  // code to execute
end;

Example:

var
  i: Integer;
begin
  for i := 1 to 5 do
  begin
    WriteLn('Iteration: ', i);
  end;
end;

While Loops

The While loop continues to execute as long as a specified condition is true:

while condition do
begin
  // code to execute
end;

Example:

var
  count: Integer;
begin
  count := 1;
  while count <= 5 do
  begin
    WriteLn('Count: ', count);
    count := count + 1;
  end;
end;

Repeat-Until Loops

The Repeat-Until loop is similar to the While loop, but it guarantees that the block of code will execute at least once:

repeat
  // code to execute
until condition;

Example:

var
  num: Integer;
begin
  num := 1;
  repeat
    WriteLn('Number: ', num);
    num := num + 1;
  until num > 5;
end;

Conclusion

In this lesson, we covered the essential control structures in Delphi, including If statements, Case statements, and various types of loops. Mastering these concepts will allow you to create more complex and dynamic applications. In the next lesson, we will explore functions and procedures in Delphi. Happy coding!