Conditionals

Master If-Then-Else statements and decision-making with 10 hands-on exercises

3 Easy 4 Medium 3 Hard ~70 min total
Related Lesson
Conditional Statements Theory
1

Positive/Negative/Zero

Easy 5 min

Problem:

Write a program to determine if a number is positive, negative, or zero.

Example Output:

Sample Run
Enter a number: -5 The number is negative
Need Help? Try These Hints First!

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
Key Concepts:
  • If-Elseif-Else structure for multiple conditions
  • Comparison operators (>, <)
  • Only one branch executes
2

Grade Checker

Easy 6 min

Problem:

Determine if a student passes or fails based on their score (passing score is 60 or above).

Sample Run
Enter your score: 75 Result: Pass
Need Help? Try These Hints First!

Solution

Algorithm GradeChecker
    var score = Input "Enter your score:"
    
    If score >= 60 Then
        Print "Result: Pass"
    Else
        Print "Result: Fail"
    Endif
Endalgorithm
Key Concepts:
  • Simple If-Else structure
  • Greater than or equal (>=) operator
  • Binary decision making
3

Largest of Three

Easy 7 min

Problem:

Find the largest of three numbers.

Sample Run
Enter first number: 15 Enter second number: 23 Enter third number: 19 The largest number is: 23
Need Help? Try These Hints First!

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
Key Concepts:
  • Multiple IF statements in sequence
  • Variable reassignment
  • Finding maximum value
4

Age Category

Easy 6 min

Problem:

Categorize age as Child (<13), Teen (13-19), Adult (20-64), or Senior (65+).

Sample Run
Enter age: 25 Category: Adult
Need Help? Try These Hints First!

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
Key Concepts:
  • Multiple Elseif statements
  • Range checking with <= operator
  • Mutually exclusive categories
5

Leap Year Checker

Medium 8 min

Problem:

Determine if a year is a leap year (divisible by 4, but not by 100 unless also by 400).

Sample Run
Enter year: 2024 2024 is a leap year
Need Help? Try These Hints First!

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
Key Concepts:
  • Complex conditional logic
  • Multiple conditions with Elseif
  • Modulo operator for divisibility
6

Grade Letter

Medium 7 min

Problem:

Convert a numerical grade to letter grade (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: <60).

Sample Run
Enter grade: 85 Letter Grade: B
Need Help? Try These Hints First!

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
Key Concepts:
  • Cascading Elseif statements
  • Range-based categorization
  • Order matters (check highest first)
7

Triangle Validator

Medium 8 min

Problem:

Check if three sides can form a valid triangle (sum of any two sides must be greater than the third).

Sample Run
Enter side 1: 3 Enter side 2: 4 Enter side 3: 5 These sides form a valid triangle
Need Help? Try These Hints First!

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
Key Concepts:
  • Logical AND to combine conditions
  • Mathematical rules in code
  • All conditions must be true
8

Voting Eligibility

Medium 6 min

Problem:

Check if someone can vote (age >= 18 AND citizen = true).

Sample Run
Enter age: 20 Are you a citizen? (yes/no): yes You are eligible to vote
Need Help? Try These Hints First!

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
Key Concepts:
  • Logical AND for multiple requirements
  • Both conditions must be true
  • String comparison with ==
9

Nested IF Example

Hard 10 min

Problem:

Use nested IF statements to check age eligibility for different licenses (16+ for car, 18+ for motorcycle with car license).

Sample Run
Enter age: 17 Have car license? (yes/no): yes You can drive a car You cannot get motorcycle license yet
Need Help? Try These Hints First!

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
Key Concepts:
  • Nested IF statements (IF within IF)
  • Multiple levels of decision making
  • Proper indentation for readability
10

Complex Discount System

Hard 12 min

Problem:

Calculate final price with tiered discounts: VIP members get 20%, regular members get 10%, purchases over $100 get extra 5%.

Sample Run
Enter purchase amount: 150 Membership (VIP/Regular/None): VIP Base discount: 20% Large purchase bonus: 5% Total discount: 25% Final price: 112.50
Need Help? Try These Hints First!

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
Key Concepts:
  • Multiple independent IF statements
  • Accumulating values across conditions
  • Real-world business logic
  • Complex decision trees
Back to Exercise Hub