Let’s begin with the Conditionals and Logic in Swift.
When the condition of an if
statement is true
, it executes a code block. So, the code block does not execute if the condition is false
.
var morning = true
if morning {
print("Rise and Shine!")
}
// Prints: Rise and Shine!
An if
statement has a partner in the form of an else
statement. Therefore, the else
statement’s code runs if the if
statement’s condition evaluates to false
.
var covid = false
if covid {
print("Stay at home.")
} else {
print("You can enjoy outside freely.")
}
// Prints: You can enjoy outside freely.
if Statement:Within a standard if
/else
statement, an else if
statement adds more requirements to check for. else if
statements can chain and only occur after an if
statement and before an else
statement.
let number = 0
if (number > 0) {
print("Number is positive.")
}
else if (number < 0) {
print("Number is negative")
}
else {
print("Number is 0.")
}
//Prints: Number is 0.
Operators for comparison Return a Boolean result by comparing the values of two operands:
<
less than>
greater than<=
less than or equal to>=
greater than or equal to==
equal to!=
not equal to1 > 10 // false
1 < 10 // true
1 >= 10 // false
1 <= 10 // true
"X" == "x" // false
"Y" != "y" // true
The ternary conditional operator, indicated by a ?
, is a shorter version of the if
/else
statement. It evaluates a single condition and executes the code preceding the :
if it is true
. On the other hand, the code after the :
is executed if the condition is false
.
var workExperience = true
workExperience ? print("Welcome as an Employee") : print("Welcome as an Intern")
// Prints: Welcome as an Employee
The switch
statement is a conditional that checks the value of an expression against a variety of different situations. When the value of the expression matches the case
, it executes. The default
statement can use when there are no matches between the case
statements and the expression.
var info = ("Drake", 22)
switch info {
case ("Drake", 22):
print("Drake is 22 years old")
case ("Lisa", 21):
print("Lisa is 21 years old")
default:
print("Not known")
}
//Prints: Drake is 22 years old
Within the case
of a switch
statement, intervals provide a range of values that are compared to an expression.
var episode = 2
var villain: String
switch episode {
case 1...3:
villain = "Emperor Palpatine"
case 4...6:
villain = "Darth Vader"
case 7...9:
villain = "Kylo Ren"
default:
villain = "Unknown"
}
print(villain)
// Prints: Emperor Palpatine
Within a switch
statement, a compound case is a single case
that has multiple values. So, these values are separated by commas and checked against the switch
statement’s expression.
let country = "Fiji"
switch country {
case "Russia", "New Zealand", "Fiji":
print("Eastern Country")
case "USA", "Argentina", "Tonga":
print("Western Country")
default:
print("Unknown Country")
}
// Prints: Eastern Country
A where
clause can use within a switch
statement to test additional conditions against an expression.
let number = 6
switch number {
case let a where a % 2 == 0:
print("\(number) is even")
case let a where a % 2 == 1:
print("\(number) is odd")
default:
print("\(number) is invalid")
}
// Prints: 6 is even
The logical NOT operator, symbolized by !
, is a prefix operator that negates the value to which it is added. When the original value is true
, it returns false
, and when the original value is false
, it returns true
.
!false // true
!true // false
The logical AND operator denoted by &&
evaluates two operands and returns a Boolean result. When both operands are true
, it returns true
, if at least one operand is false
, it returns false
.
true && true // true
true && false // false
false && true // false
false && false // false
||
denotes the logical OR operator, that evaluates two operands and returns a Boolean result. When both operands are false
, it returns false
, and when at least one operand is true
, it returns true
.
true || true // true
true || false // true
false || true // true
false || false // false
Logical operators can chain together to create more complex logical expressions. It’s wise to note that when logical operators are chained, the &&
operator takes precedence over the ||
operator and is evaluated first.
true && true || !true // true
/*
true && true is the first one to evaluate and returns true. The expression true || !true then evaluates and returns true as the final result.
*/
!true || true && false // false
/*
true && false were evaluated first and returns false. Then the expression, !true || false evaluates and returns false as the final result.
*/
Parentheses, ()
, can use to organize and control the flow of operations within a Swift logical expression. Parentheses within a logical expression override operator precedence rules, making code more readable.
// Without parentheses:
true || !false && false || false // true
// With parentheses:
(true || !false) && (false || false) // false
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.