Conditionals

Learn how to make decisions in your programs using if statements and conditional logic.

Basic If Statements
Making decisions based on conditions
Pattern:
if condition then
# statements to execute if true
endif
Every if statement must be closed with "endif". The code between "then" and "endif" runs only if the condition is true.
If-Else Statements
Handling both true and false conditions
Pattern:
if condition then
# statements if true
else
# statements if false
endif
The "else" section provides an alternative path when the condition is false.
Nested Conditionals
Using if statements inside other if statements
age is equal to 16
if age is greater than 12 then
if age is less than 20 then
print "Teenager"
else
print "Young adult"
endif
else
print "Child"
endif
Note

Proper nesting

Each nested if statement must have its own matching endif. Keep track of indentation for clarity.

Comparison Operators
All the ways to compare values

Equality

if x is equal to 5 then
if name is not equal to "John" then

Check if values are the same or different.

Size Comparison

if score is greater than 90 then
if age is less than 18 then

Compare which value is larger or smaller.

Inclusive Comparison

if grade is greater than or equal to 60 then
if count is less than or equal to 10 then

Include the boundary value in the comparison.

Boolean Values

if is_student is equal to true then
if has_permission is equal to false then

Work with true/false values directly.

Complex Conditional Patterns
Advanced techniques for decision making

Multiple Conditions (Else If Pattern)

score is equal to 85
if score is greater than or equal to 90 then
grade is equal to "A"
else
if score is greater than or equal to 80 then
grade is equal to "B"
else
if score is greater than or equal to 70 then
grade is equal to "C"
else
grade is equal to "F"
endif
endif
endif

Chain conditions together to handle multiple scenarios.

Range Checking

temperature is equal to 75
if temperature is greater than 60 then
if temperature is less than 80 then
print "Perfect weather!"
endif
endif

Check if a value falls within a specific range.

Flag Variables

found is equal to false
target is equal to 42
for i from 1 to 100
if i is equal to target then
found is equal to true
endif
endfor
if found is equal to true then
print "Found the number!"
endif

Use boolean variables to track states across your program.

Common Conditional Patterns
Frequently used decision-making patterns

Validation Pattern

if age is less than 0 then
print "Invalid age"
else
# process valid age
endif

Menu Selection

if choice is equal to 1 then
print "Option A"
else
if choice is equal to 2 then
print "Option B"
endif
endif

Error Checking

if divisor is equal to 0 then
print "Cannot divide by zero"
else
result is equal to number divided by divisor
endif

Status Checking

if is_logged_in is equal to true then
print "Welcome back!"
else
print "Please log in"
endif
Best Practices
Tip

Always close your if statements

Every "if" must have a matching "endif". Missing closures will cause errors.

Tip

Use meaningful variable names for flags

Names like "is_valid", "has_permission", "found" make conditions easier to read.

Tip

Avoid too many nested levels

Deep nesting makes code hard to follow. Consider breaking complex logic into simpler parts.

Tip

Test edge cases

Make sure your conditions handle boundary values correctly (like exactly 18, exactly 0, etc.).