Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

If-Else Statement

From Groundhog Learning
Revision as of 16:04, 21 October 2025 by Groundhog (talk | contribs) (Updated via bot)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

References