Swift Iterative Sentence Summary

Introduction

In this article, I've summarized the repetitive sentences 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. for-in statement-enumeration of sequence elements
  2. while statement --Repeat by continuation condition
  3. repeat-while statement --Repeat that guarantees first execution
  4. Suspend execution statement

1. for-in statement-enumeration of sequence elements

Control syntax for accessing elements of type (Array, Dittionary, etc.) that conform to the Sequence protocol. Repeat as many times as the number of elements while passing each element to the executable statement in sequence. In the executable statement, you can access the element through a constant with the name specified as the element name.

Statements that are repeatedly executed element by element
}

When enumerating elements of Array type with a for-in statement, the element type is Element type. When enumerating elements of Dictionary <Key, Value> type with a for-in statement, the element type is a tuple of (Key, Value) type.

2. while statement --Repeat by continuation condition

A control syntax that keeps repeating as long as the conditional expression holds. The while statement consists of a conditional expression and a statement that is repeatedly executed. The conditional expression of the while statement must return a Bool type like the if statement and for-in statement. The conditional expression is evaluated every time before the execution statement is executed. If the result is true, the iteration is continued, if false, the iteration is stopped, and the processing of the entire while statement is terminated.

Statements that are repeatedly executed while the conditional expression is satisfied
}

3. repeat-while statement --Repeat that guarantees first execution

Since the while statement is usually evaluated before execution, it may not be processed even once in some cases. However, if the repeat-while statement is used, the conditional expression is evaluated after the executable statement, so processing is always performed once regardless of the success or failure of the condition.

A statement that is always processed once and then repeatedly executed as long as the conditional expression is satisfied
}while conditional expression

4. Suspend execution statement

break statement-end of repetition

Suspends the execution statement and ends the entire repeating statement. It is used when there is no need to repeat it any more.

continue statement-continue repeating

After suspending the execution statement, the subsequent repetition is continued. Use this when you want to skip processing only in a specific scene. Use the continue statement only with the continue keyword or in combination with the label.

Label --Specify the control target of break and continue statements

Control targets can be specified using labels in break statements and continue statements. It is used when you want to exit once from a nested repetition. To make a repeating statement visible by label, add the label name: before the repeating statement.

Repeated sentence

Specify the target iteration statement by adding the label name after the break and continue keywords.

Recommended Posts

Swift Iterative Sentence Summary
[Swift] Simple screen transition summary
Swift conditional branch statement summary
[Swift] Summary about Bool type
[Swift] "for-in statement" about iterative processing
Summary