0
0
Swiftprogramming~10 mins

Semicolons are optional behavior in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Semicolons are optional behavior
Write statement 1
Optional semicolon?
YesSeparate statements
Write statement 2
End of line
Next line or end
Swift lets you end statements with or without semicolons; semicolons are only needed to separate multiple statements on the same line.
Execution Sample
Swift
let a = 5
let b = 10; let c = 15
print(a, b, c)
This code shows that semicolons are optional at line ends but required to separate multiple statements on one line.
Execution Table
StepCode LineActionSemicolon Present?Effect
1let a = 5Declare variable a with 5NoStatement ends at line end
2let b = 10; let c = 15Declare b with 10YesSemicolon separates next statement
3let b = 10; let c = 15Declare c with 15YesStatement ends at line end
4print(a, b, c)Print values of a, b, cNoStatement ends at line end
5End of codeProgram endsN/ANo more statements
💡 Reached end of code, no more statements to execute
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefined5555
bundefinedundefined101010
cundefinedundefinedundefined1515
Key Moments - 2 Insights
Why do we need a semicolon between 'let b = 10' and 'let c = 15' on the same line?
Because Swift uses semicolons to separate multiple statements on the same line, as shown in execution_table step 2 and 3.
Can we omit semicolons at the end of a single statement line?
Yes, semicolons are optional at the end of a line if there is only one statement, as shown in steps 1 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does the semicolon do?
AEnds the program
BStarts a new block
CSeparates two statements on the same line
DIs required after every statement
💡 Hint
See execution_table step 2 and 3 where two statements are separated by a semicolon
At which step does the variable 'c' get its value assigned?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check variable_tracker for 'c' value changes after each step
If we remove the semicolon between 'let b = 10' and 'let c = 15', what happens?
ASyntax error because statements are not separated
BCode runs fine, both variables assigned
COnly 'b' is assigned, 'c' is ignored
DProgram prints nothing
💡 Hint
Semicolons separate statements on the same line, see execution_table step 2
Concept Snapshot
Swift statements end with newline by default.
Semicolons are optional at line ends.
Use semicolons to separate multiple statements on one line.
No semicolon needed if only one statement per line.
Helps keep code clean and readable.
Full Transcript
In Swift, semicolons are optional at the end of a line if there is only one statement. However, if you want to write multiple statements on the same line, you must separate them with semicolons. For example, 'let a = 5' does not need a semicolon at the end. But if you write 'let b = 10; let c = 15' on the same line, the semicolon separates these two statements. This behavior allows Swift code to be clean and easy to read, while still supporting multiple statements on one line when needed. The execution table shows each step where variables are declared and how semicolons affect statement separation. The variable tracker shows the values assigned to each variable after each step. Remember, semicolons are only required to separate statements on the same line, not at the end of every line.