Semicolons are optional behavior in Swift - Time & Space Complexity
We want to see how using semicolons affects how long Swift code takes to run.
Does adding or skipping semicolons change the speed of the program?
Analyze the time complexity of the following code snippet.
var sum = 0
for i in 1...n {
sum += i
}
print(sum)
This code adds numbers from 1 to n and prints the total. Semicolons are not used here.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds numbers from 1 to n.
- How many times: It runs exactly n times, once for each number.
As n grows, the loop runs more times, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of steps grows directly with n, no matter if semicolons are used or not.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Adding semicolons makes the program run slower or faster."
[OK] Correct: Semicolons only help separate statements; they do not affect how many times the code runs or how fast it executes.
Understanding what affects time complexity helps you focus on the real parts that slow down code, not small syntax choices like semicolons.
"What if we replaced the for-loop with a recursive function? How would the time complexity change?"
Practice
Solution
Step 1: Understand Swift statement endings
Swift allows statements to end without semicolons if each is on its own line.Step 2: Identify when semicolons are necessary
Semicolons are needed only to separate multiple statements on the same line.Final Answer:
Only when multiple statements are on the same line -> Option DQuick Check:
Semicolons required = multiple statements on one line [OK]
- Thinking semicolons are always required
- Believing semicolons are needed after every statement
- Assuming semicolons are never used
Solution
Step 1: Check semicolon usage on same line
When two statements are on the same line, they must be separated by a semicolon.Step 2: Verify each option
let a = 5; let b = 10; correctly uses a semicolon between two statements on the same line.Final Answer:
let a = 5; let b = 10; -> Option AQuick Check:
Multiple statements same line = semicolon needed [OK]
- Omitting semicolon between statements on the same line
- Adding semicolons unnecessarily at line ends
- Confusing semicolon placement with other languages
let x = 3; let y = 4 print(x + y)
Solution
Step 1: Analyze semicolon usage
The semicolon separates two statements on the same line, which is valid in Swift.Step 2: Calculate the print output
x is 3 and y is 4, so x + y equals 7, which is printed.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 [OK]
- Thinking semicolon causes syntax error
- Confusing concatenation with addition
- Expecting runtime error due to semicolon
let a = 10 let b = 20 print(a + b)
Solution
Step 1: Check statement separation
Two statements are on the same line without a semicolon separating them, which is invalid in Swift.Step 2: Confirm print syntax and variable names
Print statement and variable names are correct; only missing semicolon causes error.Final Answer:
Missing semicolon between statements on the same line -> Option BQuick Check:
Multiple statements same line need semicolon [OK]
- Ignoring missing semicolon error
- Assuming print needs no parentheses
- Thinking variable names cause error
let name = "Alice" and print(name). Which is the correct way to write this?Solution
Step 1: Understand semicolon use on same line
When writing multiple statements on the same line, separate them with a semicolon.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.Final Answer:
let name = "Alice"; print(name) -> Option AQuick Check:
Separate statements on one line with semicolon [OK]
- Omitting semicolon between statements
- Adding extra semicolon after last statement unnecessarily
- Writing statements without separation
Ready to prove it?
Pass the test and this pattern moves unknown
