Swift Syntax

Syntax is the set of grammar rules that every Swift program follows. A friend forgives a missing comma in a text message. The Swift compiler never forgives one. Every keyword, symbol, and line break carries a precise meaning, and learning these rules early saves hours of confusing error messages.

Think of syntax as the traffic rules of a city. Drivers who follow the rules reach their destination safely. The compiler plays the traffic officer and stops any program that breaks a rule.

Statements and Line Breaks

A statement is one complete instruction. Swift ends a statement at the end of the line. You never type a semicolon after it. A semicolon becomes necessary only when two statements share one line.

let city = "Mumbai"
let year = 2026
let a = 1; let b = 2

Most Swift developers skip semicolons entirely. Clean line breaks keep the code easy to scan.

Diagram: Anatomy of a Statement

let   score   =   95
 |      |     |    |
 |      |     |    +-- value stored
 |      |     +------- assignment operator
 |      +------------- name (identifier)
 +-------------------- keyword that creates a constant

Every statement splits into small parts like this. The keyword tells Swift what to do, the name labels the data, and the value fills the label.

Comments

Comments are notes written for humans. The compiler ignores them completely. Swift supports three comment styles.

// A single-line comment

/* A multi-line comment
   that spreads across several lines */

/// A documentation comment
/// Xcode displays this text in Quick Help.
func greet() { }

Swift allows nested multi-line comments. You can wrap a block of code that already contains comments and switch the whole block off safely.

/* Outer comment
   /* Inner comment */
   Still inside the outer comment */

Printing Output

The print function sends text to the console. Swift places the value of a variable inside a string with a backslash and parentheses. Developers call this string interpolation.

let name = "Riya"
let score = 92
print("Player: \(name), Score: \(score)")
// Output: Player: Riya, Score: 92

print("A", "B", "C", separator: "-")
// Output: A-B-C

print("Loading", terminator: "...")
print("Done")
// Output: Loading...Done

The separator argument controls the text placed between items. The terminator argument controls what appears at the end of the output. The default terminator is a new line.

Case Sensitivity

Swift treats uppercase and lowercase letters as different characters. The names score, Score, and SCORE point to three separate items. A typo in capitalization produces an error that says the name cannot be found.

Naming Rules

A name in Swift is called an identifier. Follow these rules whenever you create one.

  • Start the name with a letter or an underscore.
  • Use letters, digits, and underscores after the opening character.
  • Avoid spaces and special symbols such as @ or #.
  • Avoid reserved keywords such as let, func, or class.

Swift accepts Unicode characters in names. The line let π = 3.14159 works. Plain English names remain the best choice for readable code.

Naming Conventions

Rules decide what the compiler accepts. Conventions decide what other developers expect. The Swift community uses two styles.

  • lowerCamelCase for variables, constants, functions, and methods, such as totalPrice and fetchData.
  • UpperCamelCase for types, such as Student and ShoppingCart.

Descriptive names beat short names. The name numberOfStudents explains itself, while n forces the reader to guess.

Whitespace and Indentation

Swift does not care about the number of spaces inside a line. Code blocks use curly braces. Indentation exists for readability only. Four spaces per level is the standard style in Xcode.

if score > 50 {
    print("Pass")
} else {
    print("Try again")
}

Common Syntax Errors

Beginners meet the same few errors again and again. Recognizing them speeds up debugging.

  • A missing closing quote in a string.
  • A missing closing brace at the end of a block.
  • A wrong capitalization in a variable name.
  • A reserved keyword used as a variable name.

Xcode underlines these problems in red and suggests a fix. Read the message slowly, since it usually names the exact line and cause.

Key Takeaways

  • Swift ends a statement at the end of the line.
  • Comments use // for one line and /* */ for many lines.
  • The print function supports separators, terminators, and string interpolation.
  • Names are case sensitive and follow camelCase conventions.