0
0
Swiftprogramming~5 mins

Semicolons are optional behavior in Swift

Choose your learning style9 modes available
Introduction

Semicolons separate statements in Swift, but you usually don't need to write them. This makes your code cleaner and easier to read.

When writing multiple statements on the same line.
When you want to clearly separate statements in a complex line.
When converting code from other languages that require semicolons.
When you prefer explicit statement endings for clarity.
Syntax
Swift
statement1
statement2

// or

statement1; statement2

Swift lets you skip semicolons if each statement is on its own line.

If you put multiple statements on one line, separate them with semicolons.

Examples
Each statement is on its own line, so no semicolons are needed.
Swift
let a = 5
let b = 10
print(a + b)
Multiple statements on one line require semicolons to separate them.
Swift
let a = 5; let b = 10; print(a + b)
Two print statements on the same line separated by a semicolon.
Swift
print("Hello"); print("World")
Sample Program

This program shows two ways to write statements: without semicolons on separate lines, and with semicolons on the same line.

Swift
let greeting = "Hello"
let name = "Swift"
print(greeting, name)

// Same code with semicolons
let greeting2 = "Hi"; let name2 = "Coder"; print(greeting2, name2)
OutputSuccess
Important Notes

Don't mix styles in the same block; choose one for readability.

Semicolons are required only when multiple statements share a line.

Summary

Swift does not require semicolons at the end of each statement if they are on separate lines.

Use semicolons only to separate multiple statements on the same line.

This feature helps keep Swift code clean and easy to read.