Swift: Operators, expressions, and control flow

Section 2.2: Swift: Operators, Expressions, and Control Flow


In this section, we'll delve into the tools that allow you to manipulate data, create logical expressions, and control the flow of your Swift programs.


Operators: Performing Operations on Data


Operators are symbols or special keywords that perform operations on values. Swift offers a wide range of operators for various tasks:


Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulo - remainder after division)

Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)

Logical Operators: && (logical AND), || (logical OR), ! (logical NOT)

Compound Assignment Operators: +=, -=, *=, /=, %=

Range Operators: ... (closed range), ..< (half-open range)

Other Operators: ?? (nil-coalescing), ??= (nil-coalescing assignment), as (type casting), is (type checking), etc.

Expressions: Combining Values and Operators


An expression is a combination of values, variables, constants, and operators that produces a result. Expressions can be as simple as a single value or as complex as a combination of multiple values and operations.


Examples:

Swift

let sum = 5 + 3         // Arithmetic expression

let isEqual = 10 == 10   // Comparison expression

let isValid = true && false // Logical expression


Control Flow: Directing Program Execution


Control flow statements allow you to control the order in which your code executes. They provide the ability to make decisions, repeat tasks, and alter the path of your program.


Conditional Statements:

if Statement: Executes a block of code if a condition is true.

else Clause: Provides an alternative block of code to execute if the if condition is false.

else if Clause: Allows for multiple conditions to be checked.

switch Statement: Provides a concise way to handle multiple possible cases.

Loop Statements:

for-in Loop: Iterates over a sequence (e.g., array, range).

while Loop: Repeats a block of code as long as a condition is true.

repeat-while Loop: Similar to while, but the condition is checked at the end of each iteration.

Control Transfer Statements:

break: Exits a loop or switch statement.

continue: Skips the rest of the current iteration of a loop and proceeds to the next iteration.

return: Exits a function and returns a value (if applicable).

Example:


Swift

let age = 18

if age >= 18 {

    print("You are eligible to vote.")

} else {

    print("You are not eligible to vote yet.")

}


for i in 1...5 {

    print(i) 

}


Key Takeaways


Operators perform operations on values.

Expressions combine values and operators to produce results.

Control flow statements determine the order of code execution.

Conditional statements (if, else, else if, switch) allow decision-making.

Loop statements (for-in, while, repeat-while) enable repetitive tasks.

Control transfer statements (break, continue, return) alter the flow of execution.

By mastering operators, expressions, and control flow, you gain the power to create dynamic and responsive Swift programs that can adapt to different situations and user inputs.


Course Syllabus