Thuta Learning
Swift
ExercisesProgrammingBeginner

Exercise Set 1: Basic Syntax Practice

SwiftLesson 3340 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Exercise Set 1: Basic Syntax Practice
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

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.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Explain the type, ownership, and runtime behavior of Exercise Set 1: Basic Syntax Practice

Core syntax or pattern

swift
// 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

  1. requirement// Task 1

    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.

  2. reason and implementlet 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.

  3. test and explainprint(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.

Execution flow
  1. requirement
  2. reason and implement
  3. 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

swift
// 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))
You should see
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
swift
// 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

What best describes the core idea of Exercise Set 1: Basic Syntax Practice?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Writing a long if-else chain instead of using a range pattern (like 60...79) in a switch statement
  • Not realizing that looping over a dictionary gives you a (key, value) tuple, and calling .values only, losing track of the key
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Hands-on Exercise

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.

You'll know it worked when: isEven(number: 4) should return true, the fruits list should print three numbered lines, and gradeCategory(score: 75) should return "Pass".

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Exercise Set 1: Basic Syntax Practice | Thuta Learning