Skip to main content
infervour.com

Back to all posts

How to Use Conditional Statements In Delphi?

Published on
5 min read
How to Use Conditional Statements In Delphi? image

Best Delphi Programming Guides to Buy in October 2025

1 The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)

The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)

BUY & SAVE
$18.99
The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)
2 Delphi, in all its glory: …For more than mere programmers

Delphi, in all its glory: …For more than mere programmers

BUY & SAVE
$34.99
Delphi, in all its glory: …For more than mere programmers
3 Coding in Delphi

Coding in Delphi

BUY & SAVE
$14.99
Coding in Delphi
4 Expert Delphi: Robust and fast cross-platform application development

Expert Delphi: Robust and fast cross-platform application development

BUY & SAVE
$45.99
Expert Delphi: Robust and fast cross-platform application development
5 Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM

Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM

BUY & SAVE
$64.99
Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM
6 Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps

Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps

BUY & SAVE
$39.99
Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps
7 More Coding in Delphi

More Coding in Delphi

BUY & SAVE
$14.99
More Coding in Delphi
8 Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging

Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging

BUY & SAVE
$26.85 $39.95
Save 33%
Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging
9 Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

BUY & SAVE
$43.13 $46.99
Save 8%
Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi
10 Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney

Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney

BUY & SAVE
$48.50
Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney
+
ONE MORE?

Conditional statements in Delphi allow you to define different courses of action based on certain conditions. Here are the main conditional statements you can use in Delphi:

  1. if statement: The if statement is used to execute a block of code if a condition is true. It follows the syntax: if condition then // code to be executed if the condition is true
  2. if-else statement: The if-else statement is used to execute one block of code if a condition is true, and another block of code if the condition is false. It follows the syntax: if condition then // code to be executed if the condition is true else // code to be executed if the condition is false
  3. if-else if-else statement: The if-else if-else statement allows you to chain multiple conditions and execute different blocks of code based on those conditions. It follows the syntax: if condition1 then // code to be executed if condition1 is true else if condition2 then // code to be executed if condition2 is true else // code to be executed if neither condition1 nor condition2 is true
  4. case statement: The case statement is used to evaluate an expression and execute specific blocks of code based on its value. It follows the syntax: case expression of value1: // code to be executed if expression equals value1 value2: // code to be executed if expression equals value2 ... else // code to be executed if expression does not match any of the specified values end;
  5. ternary operator (conditional operator): The ternary operator allows you to assign a value to a variable based on a condition. It follows the syntax: variable := (condition) ? value1 : value2;

These conditional statements allow you to control the flow of your program based on different conditions, making your code more flexible and adaptable.

What are the relational operators available in Delphi?

The relational operators available in Delphi are:

  1. = (Equals)
  2. <> (Not equals)
  3. < (Less than)
  4. (Greater than)
  5. <= (Less than or equal to)
  6. = (Greater than or equal to)

These operators are used to compare values in expressions and return a boolean result (true or false) based on the comparison.

How to write an if statement in Delphi?

To write an if statement in Delphi, you can use the following syntax:

if condition then // code to be executed if the condition is true else // code to be executed if the condition is false

Here's an example:

var num: Integer;

begin num := 10;

if num > 0 then ShowMessage('The number is positive.') else if num < 0 then ShowMessage('The number is negative.') else ShowMessage('The number is zero.'); end;

In this example, if the value of num is greater than 0, a message box will display "The number is positive." If the value is less than 0, it will display "The number is negative." And if the value is exactly 0, it will display "The number is zero."

How to use conditional statements in Delphi?

In Delphi, you can use conditional statements to control the flow of your code based on certain conditions. There are three main types of conditional statements in Delphi: if statements, case statements, and ternary operator.

  1. IF Statements: The if statement allows you to execute a block of code if a certain condition is true. The basic syntax for an if statement in Delphi is as follows:

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

Here's an example that checks if a number is positive or negative:

var num: Integer; begin num := -5; if num > 0 then ShowMessage('Number is positive') else if num < 0 then ShowMessage('Number is negative') else ShowMessage('Number is zero'); end;

  1. CASE Statements: The case statement allows you to execute different blocks of code based on the value of an expression. The basic syntax for a case statement in Delphi is as follows:

case expression of value1: begin // code to execute if expression equals value1 end; value2: begin // code to execute if expression equals value2 end; ... else begin // code to execute if none of the above conditions are met end; end;

Here's an example that determines the day of the week based on a number input:

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

  1. Ternary Operator: The ternary operator (also known as the conditional operator) allows you to assign a value to a variable based on a condition. The syntax for the ternary operator in Delphi is as follows:

variable := condition ? value_if_true : value_if_false;

Here's an example that assigns a message based on the value of a boolean variable:

var isError: Boolean; messageText: String; begin isError := True; messageText := isError ? 'Error occurred' : 'No error'; ShowMessage(messageText); end;

These are the basic ways to use conditional statements in Delphi. By using these statements effectively, you can control the flow of your program based on different conditions.