Swift Basic Syntax
Learn the fundamental syntax rules of Swift programming
📝 Swift Syntax Basics
Swift syntax is clean and readable. No semicolons needed, clear variable declarations, and intuitive function definitions make Swift perfect for beginners and professionals alike.
// Basic Swift syntax example
let greeting = "Hello, Swift!"
print(greeting)
Output:
Hello, Swift!
Swift Syntax Rules
No Semicolons
Semicolons are optional in Swift
let name = "John"
print(name) // No semicolon needed
Case Sensitive
Swift distinguishes between cases
let Name = "John"
let name = "Jane" // Different variables
Comments
Single and multi-line comments
// Single line comment
/* Multi-line
comment */
Identifiers
Names for variables and functions
let userName = "Alice"
func calculateAge() { }
🔹 Variables and Constants
Swift uses
var
for variables and
let
for constants:
// Variables (can change)
var age = 25
var name = "John"
age = 26 // This is allowed
// Constants (cannot change)
let birthYear = 1998
let country = "USA"
// birthYear = 1999 // This would cause an error
print("Name: \(name)")
print("Age: \(age)")
print("Born in: \(birthYear)")
print("Country: \(country)")
Output:
Name: John
Age: 26
Born in: 1998
Country: USA
🔹 Data Types
Swift has several built-in data types:
// String
let message: String = "Hello, World!"
// Integer
let count: Int = 42
// Double (decimal numbers)
let price: Double = 19.99
// Boolean
let isActive: Bool = true
// Character
let grade: Character = "A"
// Type inference (Swift guesses the type)
let autoString = "This is a string" // Swift knows it's String
let autoNumber = 100 // Swift knows it's Int
print("Message: \(message)")
print("Count: \(count)")
print("Price: $\(price)")
print("Active: \(isActive)")
print("Grade: \(grade)")
Output:
Message: Hello, World!
Count: 42
Price: $19.99
Active: true
Grade: A
🔹 String Interpolation
Embed variables and expressions inside strings:
let firstName = "John"
let lastName = "Doe"
let age = 30
// String interpolation with \()
let introduction = "Hi, I'm \(firstName) \(lastName) and I'm \(age) years old."
print(introduction)
// You can use expressions too
let nextYear = "Next year I'll be \(age + 1) years old."
print(nextYear)
// Mathematical expressions
let width = 10
let height = 5
let area = "The area is \(width * height) square units."
print(area)
Output:
Hi, I'm John Doe and I'm 30 years old.
Next year I'll be 31 years old.
The area is 50 square units.
🔹 Basic Operators
Swift supports common mathematical and logical operators:
// Arithmetic operators
let a = 10
let b = 3
print("Addition: \(a + b)") // 13
print("Subtraction: \(a - b)") // 7
print("Multiplication: \(a * b)") // 30
print("Division: \(a / b)") // 3
print("Remainder: \(a % b)") // 1
// Comparison operators
print("Equal: \(a == b)") // false
print("Not equal: \(a != b)") // true
print("Greater than: \(a > b)") // true
print("Less than: \(a < b)") // false
// Logical operators
let isTrue = true
let isFalse = false
print("AND: \(isTrue && isFalse)") // false
print("OR: \(isTrue || isFalse)") // true
print("NOT: \(!isTrue)") // false
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1
Equal: false
Not equal: true
Greater than: true
Less than: false
AND: false
OR: true
NOT: false
🔹 Print Function
The
print()
function displays output:
// Basic print
print("Hello, World!")
// Print multiple items
print("Name:", "John", "Age:", 25)
// Print with separator
print("Apple", "Banana", "Orange", separator: " - ")
// Print without newline
print("Loading", terminator: "")
print("...", terminator: "")
print("Done!")
// Print with custom separator and terminator
print("Item 1", "Item 2", "Item 3", separator: " | ", terminator: " END\n")
Output:
Hello, World!
Name: John Age: 25
Apple - Banana - Orange
Loading...Done!
Item 1 | Item 2 | Item 3 END