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:
- 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
- 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
- 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
- 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;
- 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:
- = (Equals)
- <> (Not equals)
- < (Less than)
- (Greater than)
- <= (Less than or equal to)
- = (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:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.
- 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:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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; |
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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; |
- 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:
1
|
variable := condition ? value_if_true : value_if_false;
|
Here's an example that assigns a message based on the value of a boolean variable:
1 2 3 4 5 6 7 8 |
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.