In Swift, there are two types of statements: simple statements and control-flow statements. Simple statements are the most common and are used to construct expressions and declarations. Control flow statements are used to control the flow of program execution. There are three types of control flow statements in Swift: loop statements, branch statements, and control pass statements.
A loop is used to repeatedly execute a block of code; Branch statements are used to execute blocks of code that meet certain conditions; Control pass statements are used to change the order in which the code is executed. Each type of control flow statement is described in detail later in the description.
Whether to place the semicolon (;) Adding to the end of the statement is optional. However, if you want to write multiple independent statements on the same line, be sure to use a semicolon.
The syntax of the statement. Statement → expression; Options. Statement → statement; Options. Statement → loop – statement; Options. Statement → branch – statement; Options. Statements → Marked as – statements. Statement → control-transport-statement; Options. Statement → statement; options
Looping statements
Depending on the particular loop condition, a loop statement allows a block of code to be executed repeatedly. Swift provides four types of looping statements: for, for-in, while, and do-while.
Break and continue statements allow you to change the control flow of a loop statement. Refer to the Break and Continue statements for these two statements.
The syntax of a loop statement. The Loop Statement – For – the Statement. Loop Statement → for-in-statement. Loop statement →WHILE statement. Loop Statement → do-while-statement
For statement
The for statement allows a counter to be incremented while a block of code is executed repeatedly.
The form of the for statement is:
1. for `initialzation`; `condition`; `increment` {
2. `statements`
3. }
Copy the code
The semicolon between initialzation, condition and increment, and the braces surrounding the loop body statements are not omitted.
The execution process of the for statement is as follows:
- Initialzation is performed only once and is usually used to declare and initialize variables that need to be used in subsequent loops.
- Evaluate the condition expression: if true, statements will be executed and go to step 3. If it is false, neither statements nor increment will be executed, and for is done.
- Evaluate the increment expression, and then go to Step 2.
Variables defined in initialzation are valid only within the scope of the for statement. The type of the value of the condition expression must follow the LogicValue protocol.
A for syntax. The For – Init opt; Expression opt; Expression The for-statement → of the Opt code block. Used For (for-init opt; Expression options; Expression options) FOR the code block →. The FOR – statement – variable declarations | expression list
The For In statement
The for-in statement allows each item in the collection (or any type that follows the Sequence protocol) to be iterated over while the code block is executed repeatedly.
The form of a for-in statement is as follows:
1. for `item` in `collection` {
2. `statements`
3. }
Copy the code
The for-in statement calls the generate method of the collection expression to get the value of a Generator type (which is a type that follows the Generator protocol) before the loop begins. Next the loop begins, calling the next method of the collection expression. If it does not return None, it is assigned to item and statements are executed, returning to the beginning of the loop; Otherwise, the item will not be assigned and statements will not be executed, and the for-in is done.
GRAMMAR OF A FOR-IN STATEMENT
for-in-statement → for pattern in expression code-block
While statement
The while statement allows a block of code to be executed repeatedly.
The form of the while statement is as follows:
1. while `condition` {
2. `statements`
3. }
Copy the code
The execution process of the while statement is as follows:
- Evaluates the condition expression: if true, go to step 2. If it is false, the while is finished.
- Execute statements and go to Step 1.
Because the value of the condition is calculated before the statements are executed, statements in a while statement may or may not be executed several times.
The type of the value of the condition expression must follow the LogicValue protocol. Condition expressions can also use optional bindings. Refer to Optional bindings to add links.
GRAMMAR OF A WHILE STATEMENT WHILE – STATEMENT – WHILE the WHILE condition code – block WHILE – condition – expression | declaration
Do – While statement
A do-while statement allows a block of code to be executed one or more times.
The form of the do-while statement is as follows:
1. do {
2. `statements`
3. } while `condition`
3. } while `condition`
Copy the code
The execution process of the do-while statement is as follows:
- Execute statements and go to Step 2.
- Evaluates the condition expression: if true, go to step 1. If it is false, the do-while execution is complete.
Since the value of the condition expression is computed after the statements expression is executed, statements in a do-while statement are executed at least once.
The type of the value of the condition expression must follow the LogicValue protocol. Condition expressions can also use optional bindings. Refer to Optional bindings to add links.
GRAMMAR OF A do-while STATEMENT → DO code-block WHILE while-condition
Branch statements
Depending on the value of one or more conditions, a branch statement allows a program to execute a specified portion of the code. Obviously, the value of the condition in the branch statement will determine how to branch and which piece of code to execute. Swift provides two types of branch statements: if statements and switch statements.
The control flow in the switch statement can be modified with the break statement. For details, see the break statement.
GRAMMAR OF A BRANCH STATEMENT BRANCH – STATEMENT → if-statement BRANCH – STATEMENT → switch- STATEMENT
If statement
Depending on the value of one or more conditions, the if statement determines which piece of code to execute.
There are two standard forms of if statements, both of which must have braces.
The first form executes the code if and only if the condition is true, as follows:
1. if `condition` {
2. `statements`
3. }
Copy the code
The second form is to add an else statement to the first form, when there is only one else statement, as follows:
1. if `condition` {
2. `statements to execute if condition is true`
3. } else {
4. `statements to execute if condition is false`
5. }
Copy the code
Also, else statements can contain if statements to form a chain to test more conditions, like this:
1. if `condition 1` {
2. `statements to execute if condition 1 is true`
3. } else if `condition 2` {
4. `statements to execute if condition 2 is true`
5. }
6. else {
7. `statements to execute if both conditions are false`
8. }
Copy the code
The value type of a condition in an if statement must follow the LogicValue protocol. Conditions can also use optional bindings. For details about how to add links, see Optional Bindings.
GRAMMAR OF AN IF STATEMENT IF – the STATEMENT – IF the IF condition code – block the else – clause opt IF – condition – expression | Declaration the else clause – else code – block | else if statement – opt
A Switch statement
Depending on the control expression of the switch statement, the switch statement determines which piece of code to execute.
The switch statement looks like this:
1. switch `control expression` { 2. case `pattern 1`: 3. `statements` 4. case `pattern 2` where `condition`: 5. `statements` 6. case `pattern 3` where `condition`, 7. `pattern 4` where `condition`: 8. `statements` 9. default: 10. `statements` 11. } Copy the code
The control expression of the switch statement is first evaluated and then matched against the pattern of each case. If the match is successful, the program will execute the statements in the corresponding case block. In addition, each case block cannot be empty, that is, there is at least one statement in each case block. If you don’t want to execute code in the matched case block, simply write a break statement inside the block.
The values that can be used as control expressions are very flexible. In addition to scalar types (e.g. Int, Character), you can use any type of value, including floating point numbers, strings, tuples, instances of custom classes, and optional types. Even the values of the members in the enumerated type and the specified range. Refer to switch in the Control Flow chapter for information on using these types in switch statements.
You can add a guard expression after the pattern. The protective expression is composed of the keyword WHERE followed by an expression that acts as an additional test condition. Therefore, statements in the corresponding case block are executed if and only if the control expression matches a pattern of a case and the protective expression is true. In the following example, the control expression will only match tuples containing two equal elements, such as (1, 1) :
1. case let (x, y) where x == y:
Copy the code
As in the above example, you can also use let (or var) statements in the pattern to bind constants (or variables). These constants (or variables) can be referenced in the corresponding protected expression and in the code in the corresponding case block. However, if there are multiple pattern matching control expressions in a case, none of these patterns can be bound to constants (or variables).
The switch statement can also contain a default block, which is executed only if no other case block matches the control expression. A switch statement can have only one default block, and it must be at the end of the switch statement.
Although the actual order in which the pattern matches are performed, and in particular the order in which the pattern is calculated, is unknown, Swift stipulates that the order in which the pattern matches in the switch statement is the same as the order in which the source code is written. Therefore, when multiple patterns have the same value and can match the control expression, the program will only execute the code in the first matching case block in the source code.
The Switch statement must be complete
In Swift, there must be at least one case block for each possible value of a control expression in a switch statement. In some cases (for example, if the expression is of type Int), you can use the default block to satisfy this requirement.
There is no implicit fall through
When the code in the matched case block finishes executing, the program terminates the switch statement, rather than proceeding to the next case block. This means that if you want to execute the next case block, you need to explicitly use fallthrough statements in the case blocks you want. For more information about fallthrough statements, see Fallthrough statements.
GRAMMAR OF A SWITCH STATEMENT switch-statement → SWITCH expression {switch-cases opt} switch-cases → switch-case The switch – cases opt switch – case – case – label statement | default – label statements switch – case – case – label; | default-label ; Case-label → case case-item-list: Case – item – the list – the pattern guard – clause opt | pattern guard – clause opt, case – item – the list default – the label – the default: Guard-clause → where guard-expression guard-expression → expression
You can precede a loop or switch statement with a label, which consists of the label name followed by a colon (:). Following the label name after break and continue explicitly changes the control flow in a loop or switch statement, passing control to the statement that specifies the label tag. For these statements, see the Break and Continue statements.
The scope of a tag is all statements after the statement that the tag tags. You may not use a tagged statement, but as long as you use it, the tag name must be unique.
For an example of using labeled statements, see labeled statements for links in the Control Flow chapter.
GRAMMAR OF A LABELED STATEMENT LABELED STATEMENT – the STATEMENT – label loop – STATEMENT | STATEMENT – label switch – the STATEMENT State-label → label-name: label-name → identifier
Control transfer statement
By unconditionally passing control from one piece of code to another, control-pass statements can change the order in which code is executed. Swift provides four types of control-passing statements: break, continue, fallthrough, and RETURN.
GRAMMAR OF A CONTROL TRANSER STATEMENT control-transfer-statement → break- STATEMENT control-transfer-statement → Continue -statement control-transfer-statement → fallthrough-statement control-transfer-statement → return-statement
Break statement
The break statement is used to terminate the execution of a loop or switch statement. To use the break statement, you can either write just the keyword break or follow the label name, as follows:
1. break 2. break `label name` Copy the code
When a break statement is followed by a label name, it can be used to terminate the execution of a loop or switch statement marked by that label.
When only break is written, execution of the switch statement or the innermost loop in the context containing the break statement is terminated.
In both cases, control is passed to the first line outside the loop or switch statement.
For examples of using the break statement, see the Control Flow chapter “Break to Add links” and “Labeled statement to Add links”.
GRAMMAR OF A BREAK STATEMENT
break-statement → break label-name opt
The Continue statement
The continue statement is used to terminate the execution of the current iteration in the loop, but does not terminate the execution of the loop. With a continue statement, you can write the keyword continue alone or you can follow the continue with the label name, as follows:
1. continue
2. continue `label name`
Copy the code
When a continue statement is followed by a label name, it can be used to terminate the execution of the current iteration in the loop marked by that label.
When only break is written, it can be used to terminate the execution of the current iteration in the innermost loop that contains the continue statement in the context.
In both cases, control is passed to the first line outside the loop.
In a for statement, after the continue statement is executed, the increment expression is still evaluated, because every time the body of the loop completes, the increment expression is evaluated.
For examples of using continue statements, see Continue to Add links and Labeled statements to Add links in the Control Flow chapter.
GRAMMAR OF A CONTINUE STATEMENT
continue-statement → continue label-name opt
Fallthrough statement
The fallthrough statement is used to pass control in the switch statement. The fallthrough statement passes control from one case in the switch statement to the next. This passing is unconditional, even if the value of the next case does not match the value of the control expression in the switch statement.
The fallthrough statement can appear in any case in the switch statement, but not in the last case block. Also, fallthrough statements cannot pass control to case blocks that use optional bindings.
For an example of using a fallthrough statement in a switch statement, refer to control Transfer Statements in the Control Flow chapter for links to be added.
GRAMMAR OF A FALLTHROUGH STATEMENT continue-statement → FALLTHROUGH
Return statement
The return statement is used to pass control over the implementation of a function or method to the caller, and the program will continue execution from the caller’s position.
When using a return statement, you can write only the keyword return, or you can follow the expression after the return, as follows:
1. return
2. return `expression`
Copy the code
When a return statement is followed by an expression, the value of the expression is returned to the caller. If the type of the expression value does not match the type expected by the caller, Swift casts the type of the expression value to the type expected by the caller before returning the expression’s value.
When you just write return, you simply pass control from the function or method to the caller without returning a value. (That is, the return type of the function or method is Void or ().)
GRAMMAR OF A RETURN STATEMENT
return-statement → return expression opt
Recommended reading
IOS APP Architecture Design (I)
Introduction to iOS Development ii – The first App
Swift 2021 Ecological Research Report
This series of articles will continue to be updated, and you can also send me a personal message to get your interview information. If you have any comments and suggestions, please leave me a message.
Please pay attention to iOS friends! Like words to a like it! Thank you very much! Thank you very much! thank you
Included: www.jianshu.com/p/523424e99…