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:

[HTML]View Plaincopy
    1. var i:int64 = 0  
    2.   
    3.   
    4. While i * i < 100000 {  
    5. &NBSP;&NBSP;&NBSP;&NBSP;I++&NBSP;&NBSP;
    6. }  
    7. &NBSP;&NBSP;
    8.   
    9. println ("i = \ (i ) ")   
    10. println (" I * i = \ (i * i) ")   



Output is 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.
Hint while true { 
    Statement group  
}


prompt


Second, do While statement
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 format:  
Do {
Statement group   The
} while loop condition
does while loop has no initialization statement, the number of cycles is unknowable, and the loop body is executed first, regardless of whether the loop condition is satisfied, and then the loop condition is judged. If the condition is satisfied, the loop body is executed and the loop is stopped if it is not satisfied.
Below is a sample code:

[HTML]View Plaincopy
    1. var i:int64 = 0
    2. bo=
    3. i++
    4. } While I * I < 100000
    5. println ("i = \ (i)")
    6. 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:

[HTML]View Plaincopy
    1. println ("N   n*n")   
    2. println ("-- -------")   
    3. for var i = 1; i < 10; i++  {   
    4.     println ("\ (i)  x \ (i)  =  \ (i * i) ")   
    5. }&NBSP;&NBSP;



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:

[HTML]View Plaincopy
    1. VAR&NBSP;X:INT32&NBSP;&NBSP;
    2. var y:int32  
    3. &NBSP;&NBSP;
    4.   
    5. for x  = 0, y =  10; x < y; x++,  y--  {  
    6.      println ("(x, y)  =  (\ (×), \ (y))")   
    7. }&NBSP;&NBSP;



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:

[HTML]View Plaincopy
    1. let numbers = [1, 2,  3, 4, 5, 6, 7, 8, 9, 10]  
    2.   
    3. &NBSP;&NBSP;
    4. println ("----for-------")   
    5. for var i = 0;  I < countelements (numbers); i++ {  
    6.     println ("count is: \ (i)")   
    7. }&NBSP;&NBSP;



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:

[HTML]View Plaincopy
    1. Let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    2. println ("----for in----")
    3. For item in numbers {
    4. println ("Count is: \ (item)")
    5. }



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.

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.