Circular statements in Swift

Source: Internet
Author: User

The Loop statement enables the program code to execute repeatedly. The SWIFT programming language supports 4 types of looping constructs: while, do while, for, and for. The for and while loops test the loop condition before the loop body is executed, while the Do while tests the loop condition after the loop body is executed. This means that the for and while loops may not even be executed once for the loop body, and the Do while will execute at least one loop body. For-in is a variant of the For loop, which is specifically designed for collection traversal.
One, while statement
The while statement is a loop structure that is judged first, in the following format:
While loop condition {
Statement Group
}
While Loop has no initialization statement, the number of cycles is not known, as long as the loop condition is satisfied, the loop will go on.
Let's look at a simple example with the following code:
var I:int64 = 0while I * I < 100000 {    I++}println ("i = \ (i)") println ("I * i = \ (i * i)")


The output results are as follows:
i = 317
I * i = 100489
The purpose of the above program code is to find the largest integer with a square number less than 100 000. There are a few points to note when using a while loop, while a while looping condition statement can only write an expression and is a Boolean expression, so if a loop variable is required in the loop body, the loop variable must be initialized before the while statement. In the example, I is assigned a value of 0, and then within the loop body must change the value of the loop variable through a statement, otherwise a dead loop will occur.
TipsA dead loop is a disaster for a single-threaded program, but in multithreaded programs, a dead loop is required, and a dead loop appears in a child thread. For example, the game design of the player input device polling, or animation program playback, all need to die loop. The following code is the general notation for the dead loop.
While true {
Statement Group
}


TipsLooping is a resource-intensive operation, how can developers test and evaluate cycle efficiency? The playground tools provided by Xcode 6 can help us achieve this goal. Open the timeline in the playground interface. After opening the playground interface to run the code, as shown, the program will be running in the right timeline to draw a line segment, the horizontal axis is the time elapsed, the longitudinal axes are the I value changes, we after the completion of the execution of the line, drag segment, view the run history of I value content. In this case, the steeper the segments in the timeline, the more efficient the execution.



Second, do While statements
The use of the Do While statement is similar to the while statement, although the Do while statement is a loop conditional structure that is later judged by the following statement format:
do {
Statement Group
} While loop condition
Does while loop has no initialization statement, the number of cycles is not known, regardless of whether the loop condition is satisfied, will first execute a loop body, and then judge the loop condition. If the condition is satisfied, the loop body is executed and the loop is stopped if it is not satisfied.
Let's look at a sample code:
var i:int64 = 0do{    i++} while  I * I < 100000println ("i = \ (i)") println ("I * i = \ (i * i)")


The output results are as follows:
i = 317
I * i = 100489
This example is the same as the example in the previous section, and it is the largest integer that finds the square number less than 100 000. The output is the same.
Third, for statement
The For statement is the most widely used and most powerful loop statement. The general format is as follows:
for initialization; cyclic conditions; iteration {
Statement Group
}
When the program executes to the for statement, the initialization statement is executed first, its function is to initialize the loop variable and other variables, then the program will see if the loop condition is satisfied, if it is satisfied, then continue to execute the loop body and calculate the iteration statement, then judge the loop condition, so repeat, until the loop condition is not satisfied when the loop. The terminating statement is typically used to change the loop condition, which can manipulate loop variables and other variables.
The following sample code is a square table program that calculates 1~9:
println ("N   n*n") println ("---------") for var i = 1; i <; i++  {    println ("\ (i) x \ (i) = \ (i * i)}}


The output results are as follows:
N n*n
---------
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81
At the beginning of the loop part of this program, the loop variable i is assigned a value of 1, each cycle to determine whether the value of I is less than 10, if true, then the loop body is executed, then I plus 1. Therefore, the final result is to print out the square of the 1~9, excluding 9.
initialization, loop conditions, and iteration parts can be empty statements (but semicolons cannot be omitted), and all three are empty when they are equal to an infinite loop.
for;; {
......
}
In the initialization and iteration sections, you can use a comma statement to perform multiple operations. The comma statement is a comma-delimited sequence of statements, as shown in the following program code:
var x:int32var y:int32for x = 0, y = 10; x < y; x + +, y--  {    println ("(x, y) = (\ (×), \ (y))}


The output results are as follows:
(x, y) = (0,10)
(x, y) = (1,9)
(x, y) = (2,8)
(x, y) = (3,7)
(x, y) = (4,6)
Four, for in statements
Swift provides a For loop--for in loop dedicated to iterating through the collection. Using a for In loop does not have to write code according to the standard routines of for, but only provides a collection to traverse.
Suppose you have an array that iterates through the array in the following way:
Let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]println ('----for-------') for var i = 0; I < countelements (numbers); i++ {    println ("Count is: \ (i)")}


The output results are as follows:
Count is:0
Count is:1
Count Is:2
Count Is:3
Count is:4
Count Is:5
Count Is:6
Count is:7
Count Is:8
Count Is:9
The above statement let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] declares and initializes a collection of 10 elements, so you only need to know when initializing an array to put elements of the same type into [...] and separated by commas (,), the collection of arrays is described in detail in the 10th chapter.
The way to iterate through an array with a in loop statement is as follows:
Let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten];p rintln ("----for in----") for item in numbers {    println ("Count is: \ (item )")}


The output results are as follows:
Count is:0
Count is:1
Count Is:2
Count Is:3
Count is:4
Count Is:5
Count Is:6
Count is:7
Count Is:8
Count Is:9

As you can see from the above example, item is a loop variable, which is declared with the Var declaration before the item, which is an implicit variable. In is followed by the collection instance, and the For In loop statement takes the element one by one in the subsequent collection and saves it to item. It can be seen that the for in statement is much simpler and easier to iterate through the collection. For other operations, however, the for-in loop is less appropriate.


For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform





Circular statements in Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.