Semicolons in Swift
No Semicolons Needed
Swift statements are terminated by a newline, so you do not need to add a semicolon at the end of each line, unlike C or Java.
Example: No Semicolons Needed
let a = 5
let b = 10
print(a + b)
Login to try C/C++/Java/PHP code in the editor
Semicolons for Same-Line Statements
If you want to write two statements on a single line, Swift requires a semicolon to separate them, since there is no newline to mark the boundary.
Example: Semicolons for Same-Line Statements
let x = 1; let y = 2; print(x + y)
Login to try C/C++/Java/PHP code in the editor
Idiomatic Style
While Swift allows a trailing semicolon on any statement, idiomatic Swift code omits it since it adds no value and clutters the code.
Note: Swift-lint style guides consistently recommend dropping semicolons entirely.
Example: Idiomatic Style
let language = "Swift"
print(language)
// no semicolons needed above, and none are idiomatic here
Login to try C/C++/Java/PHP code in the editor
- Adding semicolons out of habit from other languages; while legal, they are unnecessary and not idiomatic Swift.
- Forgetting that a semicolon IS required when writing two statements on the same line.
- Assuming Swift will silently reformat code with stray semicolons in surprising ways; extra semicolons on their own line are simply unnecessary but not an error at statement boundaries.
- Swift statements do not require a trailing semicolon.
- A semicolon is only needed to separate two statements written on the same line.
- Idiomatic Swift style omits semicolons entirely, one statement per line.
- This is different from languages like C, Java, or JavaScript where semicolons are mandatory.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: