Swift Syntax

Swift Syntax

Swift syntax is designed to be highly readable, expressive, and concise.

Unlike many older languages, it removes unnecessary characters and boilerplate code.

Understanding the basic syntax is the first step toward writing effective Swift code.

Let's explore the core rules that govern how Swift is written.


No Semicolons Required

In many programming languages like C++ or Java, you must end every statement with a semicolon ;.

In Swift, semicolons are completely optional.

The compiler automatically understands that a new line means a new statement.

Without Semicolons:

let message = "Welcome to Swift"
print(message)

However, if you want to place two statements on the same line, you must use a semicolon to separate them.


Case Sensitivity

Swift is a case-sensitive programming language.

This means that Variable and variable are treated as two entirely different entities.

You must be careful with capitalization when defining variables and calling functions.

Case Sensitivity Example:

let Name = "Akash"
let name = "Intricate"
print(Name)
print(name)

Whitespace

Swift ignores extra spaces, tabs, and empty lines.

You can use whitespace to format your code cleanly and make it more readable for humans.

However, whitespace does matter when separating keywords and variable names.

For example, let name is valid, but letname will cause an error because the compiler cannot separate the keyword from the variable.


Block Structure

Swift uses curly braces {} to group blocks of code together.

You will see curly braces used in loops, conditionals, functions, and classes.

They clearly define where a block of code begins and ends.


Exercise

Are semicolons required at the end of every line in Swift?