In this article, I've summarized the conditional branch statements that are often used in Swift.
This article was written with reference to the following information.
-Introduction to Swift Practice
A control syntax that switches the statement to be executed depending on the success or failure of the condition. It consists of a conditional expression that returns a Bool type value, followed by an executable statement enclosed in {}. If the conditional expression returns true, the statement in {} is executed, and if the conditional expression returns false, the statement in {} is skipped.
Statement executed when the conditional expression is true
}
Use when there is a statement you want to execute when the condition is not satisfied. The {} in the if statement is followed by the else keyword and the statement enclosed in {}.
Statement executed when the condition is true
} else {
Statement executed when the conditional expression is false
}
Also, the else clause can be written by connecting other if statements.
Statement executed when conditional expression 1 is true
}else if conditional expression 2{
Statement executed when conditional expression 1 is false and conditional expression 2 is true
} else {
Statement executed when both conditional expression 1 and conditional expression 2 are false
}
A conditional branch statement that performs optional binding.
Branching is performed according to the presence or absence of an optional
Statement executed if the value exists
} else {
Statement executed when the value does not exist
}
In addition, the if-let statement can retrieve multiple optional
A conditional branch statement for early exit when the condition is not met. It consists of a guard keyword, a conditional expression, an else keyword, and a statement enclosed in {}. The statement in {} is executed only when the conditional expression returns false, and the execution of the statement in {} is skipped when the conditional expression returns true. In the else clause of the guard statement, you must exit the scope that contains the guard statement. Therefore, it is guaranteed at compile time that the conditional expression of the guard statement always holds after the else clause of the guard statement. If the exit from the scope including the guard statement is not included, a compile error will occur.
Statement executed when the conditional expression is false
Need to leave the scope where the guard statement is written
}
It can also be used as a guard-let statement in a guard statement. The difference between the guard-let statement and the if-let statement is that the variables and constants declared in the guard-let statement can be used after the guard-let statement. This is because if the conditional expression is not satisfied, it goes out of scope, so the existence of variables and constants is guaranteed after the guard statement.
A control syntax that uses a pattern to switch an executable statement according to the value of a control expression. Any type of value can be specified in the control expression of the switch statement. In the switch statement, whether the control expression matches the pattern is evaluated in order from the top, and the execution statement of the matched pattern is executed. Once matched and executed, matching ends and subsequent patterns are skipped. Each pattern of the switch statement is defined by the case keyword, and the execution statement that does not match any pattern is defined by the default keyword as the default case.
case pattern 1:
Statement executed when the control expression matches pattern 1
case pattern 2:
Statement executed when the control expression matches pattern 2
default:
Statement executed when the control expression does not match any pattern
}
Also, if the cases are not covered, a compile error will occur. So you need to match all possible values of the control expression to any case.
If it does not match any of the other cases Define it with the default keyword in the case that matches. If there is a default case, there will be no unmatched case, so the default case plays a role in guaranteeing completeness. However, it is preferable to avoid preparing default cases for enumerated control expressions as much as possible and enumerate individual cases.
You can add conditions that match the case.
case pattern where conditional expression:
Statement executed when the control expression matches the pattern and satisfies the conditional expression
default:
Statement executed when the control expression does not match any pattern
}
A statement that interrupts the execution of a switch statement case. It consists of only the break keyword or a combination of the break keyword and the label described later.
A mechanism for specifying the control target of a break statement. It is used in cases where it is necessary to specify the switch statement that is the target of the break statement. To make the switch statement visible by label, add the label name: before the break statement.
switch statement
Clarify the target switch statement by adding the label name after the break keyword.
A control syntax that ends the execution of a case in a switch statement and executes the next case. The fallthrough statement consists only of the fallthrough keyword.
Recommended Posts