Conditional statements allow your programs to make decisions and execute different code based on conditions. They're like crossroads in your program where it chooses which path to take.
The basic IF statement executes code only if a condition is true:
If condition Then
# Code executes only if condition is true
Endif
Algorithm SimpleIF
var age = Input "Enter your age:"
If age >= 18 Then
Print "You are an adult!"
Print "You can vote."
Endif
Print "Program complete."
Endalgorithm
IF-ELSE provides an alternative path when the condition is false:
If condition Then
# Executes if condition is true
Else
# Executes if condition is false
Endif
Algorithm PassOrFail
var score = Input "Enter your test score:"
If score >= 60 Then
Print "✅ PASS"
Print "Congratulations!"
Else
Print "❌ FAIL"
Print "Study more and try again."
Endif
Endalgorithm
Check multiple conditions in sequence:
If condition1 Then
# Executes if condition1 is true
Else If condition2 Then
# Executes if condition2 is true (and condition1 was false)
Else If condition3 Then
# Executes if condition3 is true (and previous conditions were false)
Else
# Executes if all conditions are false
Endif
Algorithm GradeCalculator
var score = Input "Enter your score (0-100):"
Print ""
Print "Your score:", score
If score >= 90 Then
Print "Grade: A - Excellent!"
Else If score >= 80 Then
Print "Grade: B - Good job!"
Else If score >= 70 Then
Print "Grade: C - Satisfactory"
Else If score >= 60 Then
Print "Grade: D - Needs improvement"
Else
Print "Grade: F - Failed"
Endif
Endalgorithm
Combine conditions using and
, or
, and not
:
Both conditions must be true:
Algorithm CanDrive
var age = Input "Enter your age:"
var hasLicense = Input "Do you have a license? (true/false):"
If age >= 18 and hasLicense == true Then
Print "✅ You can drive legally!"
Else
Print "❌ You cannot drive yet."
If age < 18 Then
Print "Reason: Too young"
Endif
If hasLicense == false Then
Print "Reason: No license"
Endif
Endif
Endalgorithm
At least one condition must be true:
Algorithm TicketDiscount
var age = Input "Enter your age:"
If age < 12 or age >= 65 Then
Print "🎫 You qualify for a discount!"
Print "Ticket price: $5"
Else
Print "Regular ticket price: $12"
Endif
Endalgorithm
IF statements inside other IF statements for complex logic:
Algorithm MovieRating
var age = Input "Enter your age:"
var hasParent = Input "Is a parent with you? (true/false):"
Print ""
Print "=== MOVIE RATINGS ==="
If age >= 18 Then
Print "✅ You can watch:"
Print "- G rated movies"
Print "- PG rated movies"
Print "- PG-13 rated movies"
Print "- R rated movies"
Else If age >= 13 Then
Print "✅ You can watch:"
Print "- G rated movies"
Print "- PG rated movies"
Print "- PG-13 rated movies"
If hasParent == true Then
Print "- R rated movies (with parent)"
Endif
Else
Print "✅ You can watch:"
Print "- G rated movies"
If hasParent == true Then
Print "- PG rated movies (with parent)"
Endif
Endif
Endalgorithm
Algorithm ShoppingDiscount
Print "=== DISCOUNT CALCULATOR ==="
Print ""
var totalAmount = Input "Enter purchase amount:"
var isMember = Input "Are you a member? (true/false):"
var discount = 0
var discountPercent = 0
# Determine discount based on amount and membership
If totalAmount >= 100 Then
If isMember == true Then
discountPercent = 20 # 20% for members on large purchases
Else
discountPercent = 10 # 10% for non-members
Endif
Else If totalAmount >= 50 Then
If isMember == true Then
discountPercent = 10 # 10% for members
Else
discountPercent = 5 # 5% for non-members
Endif
Else
If isMember == true Then
discountPercent = 5 # 5% for members only
Endif
Endif
# Calculate final price
discount = totalAmount * (discountPercent / 100)
var finalAmount = totalAmount - discount
# Display results
Print ""
Print "=== RECEIPT ==="
Print "Original amount: $", totalAmount
Print "Discount (", discountPercent, "%): $", discount
Print "---------------"
Print "Final amount: $", finalAmount
Print "You saved: $", discount
Endalgorithm
Algorithm WeatherAdvisor
var temperature = Input "Enter temperature (°C):"
var isRaining = Input "Is it raining? (true/false):"
Print ""
Print "=== WEATHER ADVICE ==="
# Clothing advice based on temperature
If temperature < 0 Then
Print "🧥 Wear: Heavy winter coat, gloves, hat"
Else If temperature < 10 Then
Print "🧥 Wear: Warm jacket"
Else If temperature < 20 Then
Print "👕 Wear: Light jacket or sweater"
Else If temperature < 30 Then
Print "👕 Wear: T-shirt"
Else
Print "🩳 Wear: Light clothing, stay hydrated!"
Endif
# Rain advice
If isRaining == true Then
Print "☔ Don't forget: Umbrella and rain jacket!"
Endif
Endalgorithm
❌ Wrong:
If age >= 18 Then
Print "Adult"
# Missing Endif!
✅ Correct:
If age >= 18 Then
Print "Adult"
Endif
❌ Wrong (assignment):
If age = 18 Then # This assigns, doesn't compare!
✅ Correct (comparison):
If age == 18 Then # This compares
If...Then...Endif
for single conditionsElse
to provide an alternative pathElse If
to check multiple conditionsand
, or
, not
Endif
==
for comparison, =
for assignment