Bird
Raised Fist0
Swiftprogramming~15 mins

Semicolons are optional behavior in Swift - Mini Project: Build & Apply

Choose your learning style10 modes available
Semicolons are optional behavior
📖 Scenario: Imagine you are writing a simple Swift program to greet friends. Swift allows you to write code with or without semicolons at the end of each statement. This project will help you understand how semicolons work in Swift.
🎯 Goal: You will create a small Swift program that prints greetings to friends using semicolons optionally. You will see how semicolons can be used or omitted without changing the program's behavior.
📋 What You'll Learn
Create a variable with a friend's name
Create a variable with another friend's name
Print greetings to both friends using semicolons at the end of statements
Print greetings to both friends without semicolons at the end of statements
💡 Why This Matters
🌍 Real World
Knowing that semicolons are optional helps you read Swift code written by others and write your own code in a style you prefer.
💼 Career
Swift developers often write clean code without semicolons, but understanding them is important for reading legacy code or code from other languages.
Progress0 / 4 steps
1
Create variables with friend names
Create two variables called friend1 and friend2 with the exact values "Alice" and "Bob" respectively.
Swift
Hint

Use var to create variables and assign the exact string values.

2
Add semicolons to print greetings
Add two print statements to greet friend1 and friend2 using semicolons at the end of each statement. Use print("Hello, \(friend1)!"); and print("Hello, \(friend2)!"); exactly.
Swift
Hint

Remember to put a semicolon ; at the end of each print statement.

3
Rewrite print statements without semicolons
Rewrite the two print statements to greet friend1 and friend2 exactly as before but without semicolons at the end of each statement.
Swift
Hint

Simply remove the semicolons at the end of each print statement.

4
Print all greetings to show output
Add print statements to display greetings to friend1 and friend2 exactly as Hello, Alice! and Hello, Bob! without semicolons. This will show the program output.
Swift
Hint

Use print statements to show the greetings exactly as output.

Practice

(1/5)
1. In Swift, when are semicolons required at the end of statements?
easy
A. Never, semicolons are not used in Swift
B. Always at the end of every statement
C. Only after function declarations
D. Only when multiple statements are on the same line

Solution

  1. Step 1: Understand Swift statement endings

    Swift allows statements to end without semicolons if each is on its own line.
  2. Step 2: Identify when semicolons are necessary

    Semicolons are needed only to separate multiple statements on the same line.
  3. Final Answer:

    Only when multiple statements are on the same line -> Option D
  4. Quick Check:

    Semicolons required = multiple statements on one line [OK]
Hint: Semicolons separate statements on the same line only [OK]
Common Mistakes:
  • Thinking semicolons are always required
  • Believing semicolons are needed after every statement
  • Assuming semicolons are never used
2. Which of the following Swift code snippets is syntactically correct?
easy
A. let a = 5; let b = 10;
B. let a = 5 let b = 10;
C. let a = 5; let b = '10'
D. let a = 5 let b = 10

Solution

  1. Step 1: Check semicolon usage on same line

    When two statements are on the same line, they must be separated by a semicolon.
  2. Step 2: Verify each option

    let a = 5; let b = 10; correctly uses a semicolon between two statements on the same line.
  3. Final Answer:

    let a = 5; let b = 10; -> Option A
  4. Quick Check:

    Multiple statements same line = semicolon needed [OK]
Hint: Use semicolons only to separate statements on the same line [OK]
Common Mistakes:
  • Omitting semicolon between statements on the same line
  • Adding semicolons unnecessarily at line ends
  • Confusing semicolon placement with other languages
3. What is the output of this Swift code?
let x = 3; let y = 4
print(x + y)
medium
A. Syntax error
B. 34
C. 7
D. Runtime error

Solution

  1. Step 1: Analyze semicolon usage

    The semicolon separates two statements on the same line, which is valid in Swift.
  2. Step 2: Calculate the print output

    x is 3 and y is 4, so x + y equals 7, which is printed.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Semicolons separate statements; addition works normally [OK]
Common Mistakes:
  • Thinking semicolon causes syntax error
  • Confusing concatenation with addition
  • Expecting runtime error due to semicolon
4. Identify the error in this Swift code:
let a = 10 let b = 20
print(a + b)
medium
A. Variable names are invalid
B. Missing semicolon between statements on the same line
C. Missing parentheses in print statement
D. No error, code runs fine

Solution

  1. Step 1: Check statement separation

    Two statements are on the same line without a semicolon separating them, which is invalid in Swift.
  2. Step 2: Confirm print syntax and variable names

    Print statement and variable names are correct; only missing semicolon causes error.
  3. Final Answer:

    Missing semicolon between statements on the same line -> Option B
  4. Quick Check:

    Multiple statements same line need semicolon [OK]
Hint: Add semicolon between statements on the same line [OK]
Common Mistakes:
  • Ignoring missing semicolon error
  • Assuming print needs no parentheses
  • Thinking variable names cause error
5. You want to write two Swift statements on the same line: let name = "Alice" and print(name). Which is the correct way to write this?
hard
A. let name = "Alice"; print(name)
B. let name = 'Alice'; print(name);
C. let name = "Alice" print(name);
D. let name = "Alice" print(name)

Solution

  1. Step 1: Understand semicolon use on same line

    When writing multiple statements on the same line, separate them with a semicolon.
  2. Step 2: Check each option for correctness

    let name = "Alice"; print(name) correctly separates the two statements with a semicolon and omits unnecessary semicolon at the end.
  3. Final Answer:

    let name = "Alice"; print(name) -> Option A
  4. Quick Check:

    Separate statements on one line with semicolon [OK]
Hint: Use one semicolon between statements on the same line [OK]
Common Mistakes:
  • Omitting semicolon between statements
  • Adding extra semicolon after last statement unnecessarily
  • Writing statements without separation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard