0
0
Swiftprogramming~5 mins

Semicolons are optional behavior in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the role of semicolons in Swift?
Semicolons in Swift are used to separate multiple statements on the same line, but they are optional at the end of a line if there is only one statement.
Click to reveal answer
beginner
Can you write two Swift statements on the same line without semicolons?
No, when writing two or more statements on the same line, you must separate them with semicolons.
Click to reveal answer
beginner
Is this Swift code valid? <br> <code>let a = 5<br>let b = 10</code>
Yes, this code is valid because each statement is on its own line, so semicolons are not needed.
Click to reveal answer
intermediate
How do semicolons affect readability in Swift code?
Using semicolons to put multiple statements on one line can make code harder to read. Swift encourages writing one statement per line without semicolons for clarity.
Click to reveal answer
beginner
Write a Swift line with two statements using semicolons.
Example: <br> <code>let x = 3; let y = 4</code> <br> Semicolons separate the two statements on the same line.
Click to reveal answer
In Swift, when are semicolons required?
AOnly inside functions
BAt the end of every line
CWhen writing multiple statements on the same line
DNever required
Is this Swift code valid? <br> let a = 1; let b = 2
ANo, semicolons are not allowed
BYes, semicolons separate statements on the same line
CNo, statements must be on separate lines
DYes, but semicolons are optional here
What happens if you omit semicolons when writing multiple statements on the same line in Swift?
AThe statements are combined into one
BThe code compiles fine
CThe second statement is ignored
DThe compiler throws an error
Which style does Swift encourage for better readability?
AOne statement per line without semicolons
BUsing semicolons at the end of every line
CNo semicolons and multiple statements per line
DMultiple statements per line with semicolons
Can you write this Swift code without semicolons? <br> let x = 10; let y = 20
AYes, by putting each statement on its own line
BNo, semicolons are mandatory here
CYes, by removing the second statement
DNo, semicolons are always required
Explain when semicolons are needed in Swift and why they are usually optional.
Think about how Swift treats statements on separate lines versus same line.
You got /3 concepts.
    Write an example of Swift code with two statements on the same line using semicolons, and then rewrite it without semicolons on separate lines.
    Use let statements for simple variables.
    You got /2 concepts.