Swift Statements
Swift Statements
Swift code is built from statements such as declarations, expressions, and control flow (if, switch, loops).
Expression & Declaration Statements
Declarations introduce names (like variables and constants).
Expression statements evaluate an expression, such as a function call.
This example shows a constant declaration and an expression statement that prints its value.
Semicolons and Blocks
Semicolons are optional at line ends; use them only to separate multiple statements on one line.
Braces group statements into a block.
Example
let a = 1; let b = 2 // semicolons can separate multiple statements on one line
print(a + b)
if a < b {
print("a is less than b")
}
Swift does not require semicolons at the end of each line.
Use them only when writing more than one statement on the same line.
Braces { ... } form blocks that group statements.