More actions
Updated via bot |
(No difference)
|
Latest revision as of 16:04, 21 October 2025
Selects which statements to run based on a condition.
Details
An if-else statement select which statement(s) to run based on a boolean condition.
The condition is always a boolean value. It’s either true or false. According to the value, it runs one block, or the other, but never both.
🤖 Pseudocode
if condition then
option A
else
option B
The else part is optional.
We might want to run a command when a condition is satisfied, but have no alternative when it’s not.
🤖 Example: Conditional Flow
preheat oven
mix ingredients
if chocolate cake then
add cocoa powder
pour batter into pan
bake
decorate
It’s also possible to handle more than one alternative.
🤖 Pseudocode
if condition 1 then
option A
else if condition 2 then
option B
else if condition 3 then
option C
else
option D
This way, we can handle more than two paths of execution.
In these cases, the else appears at the end as a fallback alternative.
Related Concepts
Related Actions
- Is created: Write an If-Else Statement