The OR Operator
Combine conditions with ||.
1let likesCats = true
2let likesDogs = false
3if likesCats || likesDogs {
4 print("You like animals!")
5}
$ swift main.swift
You like animals!
Every line, explained
let likesCats = true- let creates a constant: a named box whose value is set once and can never change. Swift people reach for let by default: if a value never needs to change, locking it down prevents accidents.
let likesDogs = false- let creates a constant: a named box whose value is set once and can never change. Swift people reach for let by default: if a value never needs to change, locking it down prevents accidents.
if likesCats || likesDogs {- if checks a condition. Swift style: no round brackets around the condition, but the curly braces { } are always required. || means OR: the whole condition is true if at least one side is true, and true || false is true.
print("You like animals!")- print(...) shows whatever is inside its round brackets in the terminal, then moves to a new line. All lowercase, both brackets needed, and no semicolon at the end; Swift doesn't use them.
}- This } closes the if block.