Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, decision-making statements allow you to execute different blocks of code based on certain conditions. These statements help add logic to your programs. The primary decision-making statements in Swift are if
, if-else
, if-else if-else
, and switch
.
The if
statement is used to test a condition. If the condition is true, the code block inside the if
statement is executed.
let age = 20 if age >= 18 { print("You are eligible to vote.") }
The if-else
statement provides an alternative block of code that is executed if the condition in the if
statement is false.
let age = 15 if age >= 18 { print("You are eligible to vote.") } else { print("You are not eligible to vote.") }
This statement allows you to test multiple conditions. The conditions are tested in order, and once a true condition is found, the corresponding code block is executed, and the rest are skipped.
let grade = 85 if grade >= 90 { print("A grade") } else if grade >= 80 { print("B grade") } else if grade >= 70 { print("C grade") } else { print("D grade") }
The switch
statement in Swift is more powerful and flexible than in many other languages. It allows you to test a value against several possible matches, with each match leading to a specific code block.
let direction = "east" switch direction { case "north": print("Going north!") case "south": print("Going south!") case "east": print("Going east!") case "west": print("Going west!") default: print("Unknown direction") }
A few important points about switch
in Swift:
case
must be exhaustive, meaning there must be a code block for every possible value, or you must provide a default
case.switch
does not "fall through" by default. Once a matching case's code is executed, the program exits the switch
statement.switch
can match against a wide range of patterns, including ranges, tuples, and specific kinds of values.Swift provides a comprehensive set of decision-making tools. The if
, else
, and switch
statements give developers flexibility to implement logic paths in their code. It's also worth noting that Swift's switch
statement, due to its enhanced capabilities, is often favored over long chains of if-else if-else
structures.
Using if statements in Swift programming:
Description: The if
statement in Swift is used for conditional execution of code based on a given condition.
Code:
let temperature = 25 if temperature > 30 { print("It's a hot day!") } else { print("It's a pleasant day.") }
Swift if-else statements examples:
Description: The if-else
statement allows handling both true and false conditions.
Code:
let grade = 85 if grade >= 90 { print("A") } else if grade >= 80 { print("B") } else { print("C") }
Handling multiple cases with Swift switch statement:
Description: The switch
statement in Swift is used for handling multiple possible cases.
Code:
let dayOfWeek = "Monday" switch dayOfWeek { case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday": print("It's a weekday.") case "Saturday", "Sunday": print("It's the weekend.") default: print("Invalid day.") }
Using guard statements for decision-making in Swift:
Description: guard
statements are used to transfer control out of a scope if a condition is not met.
Code:
func processOrder(orderValue: Int?) { guard let order = orderValue, order > 0 else { print("Invalid order value.") return } print("Processing order with value \(order)") } processOrder(orderValue: 42)
Swift ternary operator for concise decision-making:
Description: The ternary operator ? :
provides a concise way to express simple conditional statements.
Code:
let isRaining = true let weatherMessage = isRaining ? "Bring an umbrella." : "Enjoy the weather." print(weatherMessage)
Nested if-else statements in Swift:
Description: if-else
statements can be nested to handle more complex decision-making scenarios.
Code:
let isWeekend = false let time = 14 if isWeekend { if time >= 12 { print("It's the weekend afternoon.") } else { print("It's the weekend morning.") } } else { print("It's a weekday.") }
Swift case ranges in switch statements:
Description: switch
statements in Swift can use case ranges to match a range of values.
Code:
let score = 85 switch score { case 90...100: print("A") case 80..<90: print("B") default: print("C") }
Pattern matching in Swift switch statements:
Description: Pattern matching in switch
statements allows for more flexible and expressive matching.
Code:
let value: Any = 42 switch value { case let intValue as Int: print("It's an integer: \(intValue)") case let stringValue as String: print("It's a string: \(stringValue)") default: print("Unknown type") }