Conditionals
Master If-Then-Else statements and decision-making with 10 hands-on exercises
Positive/Negative/Zero
Problem:
Write a program to determine if a number is positive, negative, or zero.
Example Output:
Solution
Algorithm CheckSign
var number = Input "Enter a number:"
If number > 0 Then
Print "The number is positive"
Elseif number < 0 Then
Print "The number is negative"
Else
Print "The number is zero"
Endif
Endalgorithm
- If-Elseif-Else structure for multiple conditions
- Comparison operators (>, <)
- Only one branch executes
Grade Checker
Problem:
Determine if a student passes or fails based on their score (passing score is 60 or above).
Solution
Algorithm GradeChecker
var score = Input "Enter your score:"
If score >= 60 Then
Print "Result: Pass"
Else
Print "Result: Fail"
Endif
Endalgorithm
- Simple If-Else structure
- Greater than or equal (>=) operator
- Binary decision making
Largest of Three
Problem:
Find the largest of three numbers.
Solution
Algorithm FindLargest
var a = Input "Enter first number:"
var b = Input "Enter second number:"
var c = Input "Enter third number:"
var largest = a
If b > largest Then
largest = b
Endif
If c > largest Then
largest = c
Endif
Print "The largest number is:", largest
Endalgorithm
- Multiple IF statements in sequence
- Variable reassignment
- Finding maximum value
Age Category
Problem:
Categorize age as Child (<13), Teen (13-19), Adult (20-64), or Senior (65+).
Solution
Algorithm AgeCategory
var age = Input "Enter age:"
If age < 13 Then
Print "Category: Child"
Elseif age <= 19 Then
Print "Category: Teen"
Elseif age <= 64 Then
Print "Category: Adult"
Else
Print "Category: Senior"
Endif
Endalgorithm
- Multiple Elseif statements
- Range checking with <= operator
- Mutually exclusive categories
Leap Year Checker
Problem:
Determine if a year is a leap year (divisible by 4, but not by 100 unless also by 400).
Solution
Algorithm LeapYearChecker
var year = Input "Enter year:"
If (year mod 400 == 0) Then
Print year, "is a leap year"
Elseif (year mod 100 == 0) Then
Print year, "is not a leap year"
Elseif (year mod 4 == 0) Then
Print year, "is a leap year"
Else
Print year, "is not a leap year"
Endif
Endalgorithm
- Complex conditional logic
- Multiple conditions with Elseif
- Modulo operator for divisibility
Grade Letter
Problem:
Convert a numerical grade to letter grade (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: <60).
Solution
Algorithm GradeLetter
var grade = Input "Enter grade:"
If grade >= 90 Then
Print "Letter Grade: A"
Elseif grade >= 80 Then
Print "Letter Grade: B"
Elseif grade >= 70 Then
Print "Letter Grade: C"
Elseif grade >= 60 Then
Print "Letter Grade: D"
Else
Print "Letter Grade: F"
Endif
Endalgorithm
- Cascading Elseif statements
- Range-based categorization
- Order matters (check highest first)
Triangle Validator
Problem:
Check if three sides can form a valid triangle (sum of any two sides must be greater than the third).
Solution
Algorithm TriangleValidator
var a = Input "Enter side 1:"
var b = Input "Enter side 2:"
var c = Input "Enter side 3:"
If (a + b > c) AND (b + c > a) AND (a + c > b) Then
Print "These sides form a valid triangle"
Else
Print "These sides do NOT form a valid triangle"
Endif
Endalgorithm
- Logical AND to combine conditions
- Mathematical rules in code
- All conditions must be true
Voting Eligibility
Problem:
Check if someone can vote (age >= 18 AND citizen = true).
Solution
Algorithm VotingEligibility
var age = Input "Enter age:"
var citizen = Input "Are you a citizen? (yes/no):"
If (age >= 18) AND (citizen == "yes") Then
Print "You are eligible to vote"
Else
Print "You are NOT eligible to vote"
Endif
Endalgorithm
- Logical AND for multiple requirements
- Both conditions must be true
- String comparison with ==
Nested IF Example
Problem:
Use nested IF statements to check age eligibility for different licenses (16+ for car, 18+ for motorcycle with car license).
Solution
Algorithm LicenseChecker
var age = Input "Enter age:"
var hasCarLicense = Input "Have car license? (yes/no):"
If age >= 16 Then
Print "You can drive a car"
If (age >= 18) AND (hasCarLicense == "yes") Then
Print "You can get motorcycle license"
Else
Print "You cannot get motorcycle license yet"
Endif
Else
Print "You cannot drive yet"
Endif
Endalgorithm
- Nested IF statements (IF within IF)
- Multiple levels of decision making
- Proper indentation for readability
Complex Discount System
Problem:
Calculate final price with tiered discounts: VIP members get 20%, regular members get 10%, purchases over $100 get extra 5%.
Solution
Algorithm DiscountSystem
var amount = Input "Enter purchase amount:"
var membership = Input "Membership (VIP/Regular/None):"
var discountPercent = 0
If membership == "VIP" Then
discountPercent = 20
Print "Base discount: 20%"
Elseif membership == "Regular" Then
discountPercent = 10
Print "Base discount: 10%"
Endif
If amount > 100 Then
discountPercent = discountPercent + 5
Print "Large purchase bonus: 5%"
Endif
var finalPrice = amount - (amount * discountPercent / 100)
Print "Total discount:", discountPercent, "%"
Print "Final price:", finalPrice
Endalgorithm
- Multiple independent IF statements
- Accumulating values across conditions
- Real-world business logic
- Complex decision trees