Any programming language must have conditional statements because they let you regulate how your program behaves under various scenarios. Multiple conditional statement types are available in Swift, Apple’s robust and user-friendly programming language, to assist developers in writing adaptable and dynamic code. We will look at the many kinds of conditional statements that Swift offers and how to utilise them in this blog post. Also we will use other details of Creating a New Class in Swift
if
Statement
The if
statement is the most basic form of conditional statement. It executes a block of code if a specified condition is true.
let number = 10
if number > 0 {
print("The number is positive.")
}
In this example, the message “The number is positive.” is printed because the condition number > 0
is true.
if-else
Statement
The if-else
statement allows you to execute one block of code if the condition is true and another block if the condition is false.
let number = -5
if number > 0 {
print("The number is positive.")
} else {
print("The number is not positive.")
}
Here, the message “The number is not positive.” is printed because the condition number > 0
is false.
else if
Statement
The else if
statement lets you test multiple conditions. If the first condition is false, it moves to the next condition, and so on.
let number = 0
if number > 0 {
print("The number is positive.")
} else if number < 0 {
print("The number is negative.")
} else {
print("The number is zero.")
}
In this example, the message “The number is zero.” is printed because neither of the first two conditions is true.
Ternary Conditional Operator
The ternary conditional operator is a shorthand way to write an if-else
statement. It is written in the form condition ? trueExpression : falseExpression
.
let number = 8
let result = (number % 2 == 0) ? "even" : "odd"
print("The number is \(result).")
This code will print “The number is even.” because the condition number % 2 == 0
is true.
switch
Statement
The switch
statement is used to compare a value against multiple possible matching patterns. It is often used as an alternative to a long if-else if-else
chain.
let letter = "a"
switch letter {
case "a", "e", "i", "o", "u":
print("\(letter) is a vowel.")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(letter) is a consonant.")
default:
print("\(letter) is not a letter.")
}
In this example, the message “a is a vowel.” is printed because the value of letter
matches the first case.
guard
Statement
The guard
statement is used to transfer program control out of a scope if one or more conditions aren’t met. It is often used for early exits in functions.
func greet(person: [String: String]) {
guard let name = person["name"] else {
print("No name provided.")
return
}
print("Hello, \(name)!")
}
greet(person: ["name": "John"])
greet(person: [:])
This will output:
Hello, John!
No name provided.
In the first call to greet
, the name
key is present in the dictionary, so the guard statement allows the function to continue. In the second call, the name
key is missing, so the guard statement prints an error message and exits the function early.
Conclusion
Swift provides a variety of conditional statements to handle different scenarios in your code. Understanding how to use if
, if-else
, else if
, ternary operators, switch
, and guard
statements will enable you to write more flexible and robust programs. Experiment with these conditional statements in your Swift projects to see how they can help you control the flow of your code more effectively.