Let's think about this for a second
This lesson isn't new teaching content — it's an exercise set meant for hands-on practice of the syntax fundamentals you learned in the Basics chapter. Each task focuses on a single concept, and you'll only benefit if you write the code yourself instead of copy-pasting. The difficulty is kept simple as a warm-up; Exercise Set 2 steps up to Intermediate/Advanced level. Once you finish the tasks below, run the code and check your predicted output against the actual result.
Exercises
Task 1: Write a function isEven(number: Int) -> Bool that returns true for even numbers. Task 2: Build a [String] array called fruits and use a for-in loop to print each item with its index in the format "1. Apple". Task 3: Build a [String: Int] dictionary called scores (student name to score) and loop through it to calculate the average. Task 4: Write a function using a switch statement that classifies an integer input into a category — 0-59 fail, 60-79 pass, 80-100 excellent.
Sample Code
// Task 1
func isEven(number: Int) -> Bool {
// TODO: implement
return false
}
// Task 2
let fruits = ["Apple", "Banana", "Mango"]
// TODO: for-in loop with index
// Task 3
let scores: [String: Int] = ["Aung": 80, "Su": 65, "Ko Ko": 90]
// TODO: calculate average
// Task 4
func gradeCategory(score: Int) -> String {
// TODO: switch statement with ranges
return ""
}
print(isEven(number: 4))
print(gradeCategory(score: 75))isEven(number: 4) should return true, the fruits list should print three numbered lines, and gradeCategory(score: 75) should return "Pass".Give it 5 minutes
After finishing Task 4, try feeding in a negative score (-5) and add a default case so it returns "Invalid" for invalid cases. Try solving it within 5 minutes.
One thing to watch out for
Try each task on your own for about 5 minutes before checking the solution — reading error messages yourself and debugging is the practice that actually cements the Basics concepts.