Explain it simply
Exercise Set 1: Basic Syntax Practice, in plain language
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.
Problem → Why → Solution
The problem — why this concept matters
Writing a long if-else chain instead of using a range pattern (like 60...79) in a switch statement
A common mistake
Not realizing that looping over a dictionary gives you a (key, value) tuple, and calling .values only, losing track of the key
Core concept — understand how it works
Build Exercise Set 1: Basic Syntax Practice in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- requirement
- reason and implement
- test and explain
Explain the type, ownership, and runtime behavior of Exercise Set 1: Basic Syntax Practice
Core syntax or pattern
// 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))Build Exercise Set 1: Basic Syntax Practice in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
Read the code step by step
- requirement
// Task 1This 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.
- reason and implement
let scores: [String: Int] = ["Aung": 80, "Su": 65, "K...Build Exercise Set 1: Basic Syntax Practice in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- test and explain
print(gradeCategory(score: 75))Run and test an original Exercise Set 1: Basic Syntax Practice example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- requirement
- reason and implement
- test and explain
Success
Explain the type, ownership, and runtime behavior of Exercise Set 1: Basic Syntax Practice
Failure
Writing a long if-else chain instead of using a range pattern (like 60...79) in a switch statement
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.
Real-world usage
How to apply it in a real project
Run and test an original Exercise Set 1: Basic Syntax Practice example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Exercise Set 1: Basic Syntax Practice example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// 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))Expected: Explain the type, ownership, and runtime behavior of Exercise Set 1: Basic Syntax Practice
Show hint
Run and test an original Exercise Set 1: Basic Syntax Practice example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- requirement → reason and implement → test and explain
- Build Exercise Set 1: Basic Syntax Practice in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- Writing a long if-else chain instead of using a range pattern (like 60...79) in a switch statement
- Run and test an original Exercise Set 1: Basic Syntax Practice example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org