Swift Syntax

Learn the basic rules and structure of Swift code, including how statements and print work.

Basic Structure

Swift code is organized into statements, which are typically written one per line. Unlike languages such as C, Java, or JavaScript, Swift does not require a semicolon at the end of each statement — the end of a line is enough to signal the end of a statement.

No Semicolons Needed

let city = "Chennai"
let country = "India"
print(city)
print(country)

Semicolons Are Optional (For Multiple Statements)

You can still use a semicolon if you want to put multiple statements on a single line. This is allowed but not commonly used, since Swift's style favors one statement per line for readability.

Multiple Statements on One Line

let a = 5; let b = 10; print(a + b) // 15

Case Sensitivity and Naming

Swift is case-sensitive, so `myValue` and `MyValue` are treated as two different identifiers. Variable and constant names typically use camelCase (starting with a lowercase letter), while type names such as classes and structs use PascalCase (starting with an uppercase letter).

  • Identifiers can contain letters, digits, and underscores, but cannot start with a digit
  • Swift keywords like `let`, `var`, and `func` are reserved and cannot be used as names
  • Blocks of code are grouped using curly braces `{ }`
  • Whitespace and indentation improve readability but are not syntactically required
  • Comments are ignored entirely by the compiler

Blocks and Braces

func greet(name: String) {
    let message = "Hi, \(name)!"
    print(message)
}

greet(name: "Aditi")
FeatureSwift Behavior
Statement terminatorNewline (semicolon optional)
Case sensitivityYes, `Age` and `age` are different
Code blocksDefined using `{ }`
Naming conventioncamelCase for variables, PascalCase for types
Note: Because semicolons are optional, most Swift style guides recommend leaving them out entirely unless writing multiple statements on one line.
Note: Even though Swift doesn't require semicolons, forgetting a closing brace `}` or parenthesis `)` will still cause a compile error — braces and parentheses must always be balanced.

Exercise: Swift Syntax

Is Swift a case-sensitive language?