DEV Community

panfan
panfan

Posted on

Understanding Control Flow and Functions in Swift

In this unit, we will delve into the concepts of control flow and functions in Swift. These are fundamental aspects of any programming language, and Swift is no exception. By the end of this unit, you will be able to write your own functions and control the flow of your code effectively.

Control Flow

Control flow in Swift is handled by using different control structures including loops and conditionals.

Loops

Loops are used when a block of code needs to be executed repeatedly. Swift provides several ways to write loops:

  • For-In Loops: This loop is used to iterate over a sequence like an array, range, string, etc.
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
Enter fullscreen mode Exit fullscreen mode
  • While Loops: A while loop performs a set of statements until a condition becomes false.
var i = 1
while i <= 5 {
    print(i)
    i += 1
}
Enter fullscreen mode Exit fullscreen mode
  • Repeat-While Loops: Similar to a while loop, but the condition is evaluated after the loop execution.
var i = 1
repeat {
    print(i)
    i += 1
} while i <= 5
Enter fullscreen mode Exit fullscreen mode

Conditionals

Conditionals are used to perform different actions based on different conditions. Swift provides two ways to write conditionals:

  • If Statement: It is used to test a condition. If the condition is true, then the code within the if statement is executed.
var temperature = 30
if temperature > 25 {
    print("It's hot!")
}
Enter fullscreen mode Exit fullscreen mode
  • Switch Statement: A switch statement allows a variable to be tested for equality against a list of values. var weather = "Sunny" switch weather { case "Sunny": print("Wear sunglasses") case "Rainy": print("Take an umbrella") default: print("Enjoy the weather!") }
## Functions
Functions are self-contained chunks of code that perform a specific task. You define a function with the func keyword.
Enter fullscreen mode Exit fullscreen mode


swift
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}
print(greet(person: "Anna"))

In this example, greet is a function that takes a parameter person of type String and returns a String.

## Closures
Closures are self-contained blocks of functionality that can be passed around and used in your code. They are similar to functions, but they don't have a name or a function definition.
Enter fullscreen mode Exit fullscreen mode


swift
let numbers = [1, 2, 3, 4, 5]
let doubledNumbers = numbers.map({ (number: Int) -> Int in
return number * 2
})
print(doubledNumbers)



In this example, { (number: Int) -> Int in return number * 2 } is a closure that doubles the value of each item in the numbers array.

By understanding control flow and functions, you can write more complex and efficient Swift code. In the next unit, we will explore Swift collections and data structures.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)