← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 7 of 7

Semicolons in Swift

Semicolons are the little dots-with-a-tail that some languages need at the end of every line, but Swift usually lets you skip them.
Syntax
swift
statement1
statement2

statement1; statement2   // semicolon only to share a line

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

markup
// Declare a constant `a`, set to `5`
let a = 5
// Declare a constant `b`, set to `10`
let b = 10
// Print `a + b` to the console
print(a + b)

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

markup
let x = 1; let y = 2; print(x + y)

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

markup
let language = "Swift"
print(language)
// no semicolons needed above, and none are idiomatic here
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Adding semicolons out of habit from other languages; while legal, they are unnecessary and not idiomatic Swift.
  2. Forgetting that a semicolon IS required when writing two statements on the same line.
  3. 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.
Chapter Summary
  • 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.