Swift If, If Else ExampleUse an if-else statement. Conditions in an if evaluate to true or false.
dot net perls

If, else. In the summer air the bee moves about. It moves left, then right and then down. It lands on a leaf. The bee makes decisions—it evaluates conditions.

In Swift, we find traditional C-like if-statements. We use "else if" after an if and follow up with an else block. This is standard.

First example. This program uses an if-statement. It tests the var also with an else-if and an else. The size is 10, so the else-if part matches.

Tip No parentheses are required around the expression being tested in an if-statement.

Tip 2 The else-if and else can be on the same line as the closing brace, or a newline. Either is accepted.

Swift program that uses if-statement, else if
var size = 10 // Test size var in if, else-if block. if size >= 20 { print("Big") } else if size >= 10 { print("Medium") } else { print("Small") }
Medium

Ternary. Swift supports a ternary statement. This uses a question mark after an initial condition. If the condition is true, the first value after the question mark is used.

And If the condition is false, the second value is used. Here we assign an Int based on the argument to the test() func.

Swift program that uses ternary statement
func test(x: Int) { // Use a ternary statement. // Set y to 5 if x is 10. // ... Set it to 0 otherwise. let y = x == 10 ? 5 : 0 print(y) } // Call ternary test method. test(x: 10) test(x: 200)
5 0

If let, optional binding. A constant can be introduced in an if-statement. Here we use the "if let" construct to look up a value in a dictionary.

Optional

Dictionary

Nil If the value exists (is not nil) the inner statements of the if are reached. We can access the constant directly.

However If the dictionary has no data at that key (the value is nil) the inner statements of the if are not reached.

Swift program that uses if let, optional binding
let animals = ["cat": 5, "dog": 2] // Execute inner block if constant is not nil. if let catCount = animals["cat"] { // This gives us a non-nil dictionary value. print(catCount) } if let birdCount = animals["bird"] { // The value is nil, so this is not reached. print(birdCount) } else { print(0) // Reached. }
5 0

Bool, true and false. A bool can store the result of an expression (one that evaluates to true or false). With an if-statement, we can then test the bool. This simplifies some programs.

Here We store the result of the even() method in constant bools. We then test those constants against true and false.

Swift program that uses bool, true, false
func even(left: Int, right: Int) -> Bool { // Return a bool if the two arguments are equal. return left == right } // Get a bool result. let even1 = even(left: 10, right: 20) if even1 == false { print("Sides not even") } // Use bool type. let even2: Bool = even(left: 40, right: 40) if even2 == true { print("Sides are even") }
Sides not even Sides are even

Guard. This statement is like an if-statement, but it must return or break. It is used to validate arguments. It helps enforce a program's correctness at runtime.

Guard

A summary. Ifs are used in nearly every program. They encode simple condition tests better than switches. But switches allow more complex, detailed branches.

Home
© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.