Operators, expressions, and control flow
Section 1.4: Operators, Expressions, and Control Flow
In this section, we'll dive into the essential tools that allow you to manipulate data, make decisions, and control the flow of your Swift programs. We'll cover operators, expressions, and various control flow statements that empower you to create dynamic and interactive applications.
Operators: Performing Actions on Data
Operators are special symbols or keywords that perform operations on values. Swift offers a wide range of operators for arithmetic, comparison, logic, assignment, and more.
Arithmetic Operators: +, -, *, /, % (modulo)
Comparison Operators: ==, !=, >, <, >=, <=
Logical Operators: && (and), || (or), ! (not)
Assignment Operators: =, +=, -=, *=, /=, %=
Range Operators: ..., ..<
Examples:
Swift
let sum = 5 + 3 // Addition
let difference = 10 - 2 // Subtraction
let product = 4 * 6 // Multiplication
let quotient = 15 / 3 // Division
let remainder = 17 % 5 // Modulo (remainder after division)
let isEqual = 5 == 5 // Equal to
let isGreater = 10 > 8 // Greater than
let isTrue = !false // Logical NOT
Expressions: Combining Values and Operators
An expression is a combination of values, variables, constants, and operators that evaluates to a single value. Expressions are the building blocks of computations in Swift.
Examples:
Swift
let area = length * width
let isValid = age >= 18 && hasLicense
Control Flow: Directing Your Code's Path
Control flow statements determine the order in which your code executes. They allow you to make decisions, repeat tasks, and alter the flow based on conditions.
Conditional Statements:
if: Executes a block of code if a condition is true.
else: Provides an alternative block of code if the if condition is false.
else if: Allows you to check multiple conditions.
switch: Provides a concise way to handle multiple possible cases.
Example:
Swift
if temperature > 80 {
print("It's a hot day!")
} else if temperature > 60 {
print("It's a pleasant day.")
} else {
print("It's a bit chilly.")
}
Loop Statements:
for-in: Iterates over a sequence (e.g., array, range).
while: Repeats a block of code as long as a condition is true.
repeat-while: Similar to while, but the condition is checked at the end of each iteration.
Example:
Swift
for i in 1...5 {
print(i)
}
var count = 0
while count < 10 {
print(count)
count += 1
}
Key Takeaways:
Operators are symbols that perform operations on values.
Expressions are combinations of values and operators.
Control flow statements direct the flow of execution in your code.
Conditional statements allow you to make decisions based on conditions.
Loop statements enable you to repeat tasks multiple times.
By mastering operators, expressions, and control flow, you gain the power to create dynamic and responsive Swift applications that can adapt to different situations and user inputs. These fundamental concepts are essential building blocks for more complex logic and algorithms as you progress in your Swift development journey.