Conditional statements are fundamental in programming as they allow you to make decisions based on certain conditions. In Delphi/Object Pascal, conditional statements help control the flow of the program by executing different code blocks based on specific conditions.

Key Concepts

  1. If-Then Statement: Executes a block of code if a specified condition is true.
  2. If-Then-Else Statement: Executes one block of code if a condition is true and another block if the condition is false.
  3. Case Statement: Selects one of many code blocks to execute based on the value of an expression.

If-Then Statement

The if-then statement is the simplest form of conditional statement. It evaluates a condition and executes a block of code if the condition is true.

Syntax

if condition then
  statement;

Example

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

Explanation

  • age is a variable of type Integer.
  • The if statement checks if age is greater than or equal to 18.
  • If the condition is true, it executes WriteLn('You are an adult.');.

If-Then-Else Statement

The if-then-else statement provides an alternative block of code to execute if the condition is false.

Syntax

if condition then
  statement1
else
  statement2;

Example

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

Explanation

  • The if statement checks if age is greater than or equal to 18.
  • If the condition is true, it executes WriteLn('You are an adult.');.
  • If the condition is false, it executes WriteLn('You are a minor.');.

Case Statement

The case statement is used when you have multiple conditions to check. It is more readable and efficient than multiple if-then-else statements.

Syntax

case expression of
  value1: statement1;
  value2: statement2;
  ...
  else statementN;
end;

Example

var
  grade: Char;
begin
  grade := 'B';
  case grade of
    'A': WriteLn('Excellent!');
    'B': WriteLn('Good job!');
    'C': WriteLn('Well done!');
    'D': WriteLn('You passed.');
    'F': WriteLn('Better try again.');
  else
    WriteLn('Invalid grade.');
  end;
end.

Explanation

  • The case statement checks the value of grade.
  • It executes the corresponding WriteLn statement based on the value of grade.
  • If grade does not match any of the specified values, it executes the else block.

Practical Exercises

Exercise 1: Simple If-Then Statement

Write a program that checks if a number is positive and prints "Positive number" if true.

Solution

var
  number: Integer;
begin
  number := 10;
  if number > 0 then
    WriteLn('Positive number');
end.

Exercise 2: If-Then-Else Statement

Write a program that checks if a number is even or odd and prints the result.

Solution

var
  number: Integer;
begin
  number := 7;
  if number mod 2 = 0 then
    WriteLn('Even number')
  else
    WriteLn('Odd number');
end.

Exercise 3: Case Statement

Write a program that takes a day of the week (1-7) and prints the corresponding day name.

Solution

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

Common Mistakes and Tips

  • Forgetting the else block: Ensure you handle both true and false conditions when using if-then-else.
  • Using = instead of :=: Remember that = is for comparison, while := is for assignment.
  • Not using begin and end for multiple statements: If you have multiple statements in an if or else block, enclose them in begin and end.

Conclusion

In this section, you learned about conditional statements in Delphi/Object Pascal, including if-then, if-then-else, and case statements. These constructs allow you to control the flow of your program based on different conditions. Practice the exercises to reinforce your understanding and prepare for more complex control structures in the next section.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved