Mastering Control Flow in Swift: A Guide to For Loops, If Statements, and Switch Statements

Ferdous Mahmud Akash

Ferdous Mahmud Akash

· 3 min read
For, If, and switch statements in Swift

For Loops

For loops in Swift are used to iterate over collections of values or to perform a certain operation a specific number of times.
Here are some reasons why you might want to use a for loop in Swift:

  • Iterating over arrays
  • Processing ranges of numbers
  • Implementing algorithms

For-In Loop

The most common type of for loop in Swift is the for-in loop. It is used to iterate over a collection of values, such as an array or a range of numbers. Here's an example:

let fruits = ["apple", "banana", "orange"]

for fruit in fruits {
    print(fruit)
}

This code will print each fruit in the array to the console. You can also use a for-in loop to iterate over a range of numbers:

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

This code will print the numbers from 1 to 5 to the console.

For-Each Loop

The for-each loop is similar to the for-in loop, but it is used with closures to perform an operation on each element in a collection. Here's an example:

let numbers = [1, 2, 3, 4, 5]

numbers.forEach { number in
    print(number * 2)
}

For Loop with Stride

A for loop with stride is used to iterate over a range of numbers with a specific step value. Here's an example:

for i in stride(from: 0, to: 10, by: 2) {
    print(i)
}

If Statements

An if statement is used to conditionally execute a block of code. In Swift, you can use an if statement to check if a condition is true, like this:

let age = 25

if age >= 18 {
    print("You are an adult")
}

This prints "You are an adult" to the console if the age variable is greater than or equal to 18.

You can also use an if statement with an else clause to execute a different block of code if the condition is false:

let password = "secret"

if password == "secret" {
    print("Access granted!")
} else {
    print("Access denied!")
}

This prints "Access granted" to the console if the password variable is equal to "secret", and "Access denied" otherwise.

Switch Statements

A switch statement is used to match a value against a set of possible cases. In Swift, you can use a switch statement to match against an enumeration, a value of a specific type, or a range of values.

let direction = "north"

switch direction {
case "north":
    print("Go up")
case "south":
    print("Go down")
case "east":
    print("Go right")
case "west":
    print("Go left")
default:
    print("Unknown direction")
}

This prints "Go up" to the console if the direction variable is equal to "north", and executes the corresponding block of code for the other cases. The default case is executed if none of the cases match.

Conclusion

For loops, if statements and switch statements are essential tools in Swift that allow you to write code that is both flexible and expressive. By mastering these concepts, you'll be able to write more efficient, scalable, and elegant Swift code.

So don't hesitate to start experimenting with for loops, if statements, and switch statements in your own projects, and see how they can help you build better software. Happy coding!

Ferdous Mahmud Akash

About Ferdous Mahmud Akash

Hey there! I'm a self-taught iOS developer from Bangladesh.

I'm also passionate about sharing my knowledge and experience with other aspiring developers through my blog at ferdousmahmud.co

Thank you for visiting my website, and feel free to reach out if you have any questions or just want to say hello!

Copyright © 2021-2024 Ferdous Mahmud Akash. All rights reserved.
Made by Ferdous· Github
LinkTree