Conditional Examples
Learn how to make decisions in your programs with if statements and conditional logic.
Age Classification
Classify people by age using if statements
age is equal to 18
if age is less than 13 then
print "Child"
endif
if age is greater than 12 then
if age is less than 20 then
print "Teenager"
endif
endif
if age is greater than 19 then
print "Adult"
endifTemperature Check
Weather advice based on temperature using if-else
temperature is equal to 75
if temperature is greater than 30 then
print "It's hot! Stay hydrated."
else
print "Weather is pleasant."
endif
if temperature is less than 0 then
print "It's freezing! Wear warm clothes."
else
print "No need for heavy winter gear."
endifGrade Calculator
Assign letter grades based on numerical scores
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
if score is greater than or equal to 60 then
grade is equal to "D"
else
grade is equal to "F"
endif
endif
endif
endif
print "Score: " + score
print "Grade: " + gradeNumber Comparison
Compare two numbers using multiple conditions
a is equal to 15
b is equal to 10
if a is equal to b then
print "Numbers are equal"
else
if a is greater than b then
print a + " is greater than " + b
else
print a + " is less than " + b
endif
endif
if a is not equal to b then
difference is equal to a minus b
if difference is greater than 0 then
print "Difference: " + difference
else
difference is equal to b minus a
print "Difference: " + difference
endif
endifComparison Operators Reference
All the comparison operators you can use in conditional statements
Equality
is equal to - Checks if values are the same
is not equal to - Checks if values are different
Magnitude
is greater than - Checks if first value is larger
is less than - Checks if first value is smaller
Inclusive Comparisons
is greater than or equal to
is less than or equal to
Conditional Structure
if ... then - Start a condition
else - Alternative branch
endif - End the condition
Tips for Writing Conditionals
Tip
Always close your if statements
Every "if" statement must have a corresponding "endif".
Tip
Use nested conditions carefully
Too many nested if statements can make code hard to read. Consider alternatives.
Tip
Test edge cases
Make sure your conditions handle boundary values correctly.