Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources
Control Flow in Swift

Control Flow in Swift

Introduction to Control Flow

Control flow in programming refers to the order in which individual statements, instructions, or function calls are executed or evaluated. In Swift, control flow is essential for making decisions in your code and enabling different paths of execution based on conditions.

Conditional Statements

Conditional statements allow you to execute different pieces of code based on certain conditions. The most commonly used conditional statements in Swift are if, else if, and else.

Example: Using if-else Statements

Here’s a simple example of how to use an if statement:

if temperature > 30 { print("It's a hot day!") } else { print("The weather is nice.") }

In this example, if the variable temperature is greater than 30, it prints "It's a hot day!", otherwise it prints "The weather is nice."

Switch Statements

The switch statement in Swift is a powerful control flow construct that allows you to evaluate a value against several possible matching patterns. It is often more readable than using multiple if statements.

Example: Using switch Statement

Here's how a switch statement works:

let day = "Monday" switch day { case "Monday": print("Start of the work week!") case "Friday": print("Almost the weekend!") default: print("Just another day.") }

This example checks the value of day and prints a message accordingly.

Loops

Loops are used to execute a block of code repeatedly. Swift provides several types of loops: for, while, and repeat-while.

Example: Using for Loop

The for loop is commonly used for iterating over a sequence:

for number in 1...5 { print(number) }

This loop iterates over the numbers from 1 to 5 and prints each number.

Example: Using while Loop

The while loop continues executing as long as a condition is true:

var count = 1 while count <= 5 { print(count) count += 1 }

This loop prints numbers from 1 to 5 using a while statement.

Control Transfer Statements

Control transfer statements allow you to change the flow of control in your loops or switch statements. Key control transfer statements in Swift include break, continue, and fallthrough.

Example: Using break Statement

The break statement is used to exit a loop or switch statement:

for number in 1...10 { if number == 5 { break } print(number) }

This loop will print numbers from 1 to 4 because it breaks when number equals 5.

Example: Using continue Statement

The continue statement skips the current iteration and moves to the next:

for number in 1...5 { if number == 3 { continue } print(number) }

This loop will print 1, 2, 4, and 5, skipping the number 3.

Conclusion

Control flow is a fundamental concept in programming that allows developers to dictate how their code executes based on conditions. In Swift, mastering control flow statements such as if, switch, loops, and control transfer statements is crucial for writing effective and efficient code.