Swift conditional branch statement summary

Introduction

In this article, I've summarized the conditional branch statements that are often used in Swift.

References

This article was written with reference to the following information.

-Introduction to Swift Practice

table of contents

  1. if statement --Branch depending on the success or failure of the condition
  2. guard statement-A branch that exits early when conditions are not met
  3. switch statement-branch by multiple pattern matching

1. if statement --Branch depending on the success or failure of the condition

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
}

-else clause-Processing when the condition is not satisfied

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
}

-if-let statement --Branch with or without value

A conditional branch statement that performs optional binding. Branching is performed according to the presence or absence of an optional type value, and if a value exists, the value is also fetched at the same time. As shown below, describe a constant definition that has an optional type value on the right side in the conditional expression. Execute the statement in {} only if the value on the right side exists, and skip the execution of the statement in {} if it is nil. To add a statement that executes only in the case of nil, add an else clause like the if statement.

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 type values ​​at the same time. In this case, the executable statement of the if statement is executed only when all the right-hand sides have values.

2. guard statement-A branch that exits early when conditions are not met

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.

3. switch statement-branch by multiple pattern matching

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.

default keyword-guaranteed completeness with default cases

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.

where keyword-adding a condition that matches the case

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
}

break statement-breaking case execution

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.

label

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.

fallthrough statement

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

Swift conditional branch statement summary
[Ruby] conditional branch if statement
Java conditional branch
java conditional branch
[Java] Conditional branch
Swift Iterative Sentence Summary
[Swift] Summary about Bool type
[Ruby] Conditional bifurcation unless statement
Conditional branch with helper method
[Java] Branch enum with switch statement
[Swift] "for-in statement" about iterative processing
Java study # 4 (conditional branching / if statement)
About if statement and branch processing