Loop statements in Swift and Swift loop statements
Loop statements allow repeated execution of program code. The Swift programming language supports four types of loop structures: while, do while, for, and for in. For and while loops test the cyclic conditions before the cyclic body is executed, while do while tests the cyclic conditions after the cyclic body is executed. This means that the for and while loops may not be executed even once, while the do while loop will be executed at least once. For in is the deformation of the for loop, which is specially designed for the Collection traversal.
1. while statement
The while statement is a loop structure that is judged first, in the following format:
While loop condition {
Statement Group
}
The while loop has no initialization statement, and the number of cycles is unknown. As long as the loop condition is met, the loop continues.
The following is a simple example. The Code is as follows:
var i:Int64 = 0while i * i < 100000 { i++}println("i = \(i)")println("i * i = \(i * i)")
The output result is as follows:
I = 317.
I x I = 100489
The purpose of the above Code is to find the maximum integer with the number of workers less than 100 000. When using the while LOOP, pay attention to the following points: In the while LOOP Condition Statement, only one expression can be written and a Boolean expression can be used. If the loop body needs to loop variables, the loop variable must be initialized before the while statement. In this example, assign a value of 0 to I first, and then use the statement to change the value of the Loop Variable inside the loop body. Otherwise, an endless loop will occur.
It indicates that an endless loop is a disaster for a Single-threaded program. However, in a multi-threaded program, an endless loop is required and will appear in the Child thread. For example, polling the player input device in the game design or playing the animation program requires an endless loop. The following code is a general method of writing an endless loop.
While true {
Statement Group
}
The prompt loop is a resource-consuming operation. How can developers test and evaluate the cycle efficiency? Playground tools provided by Xcode 6 can help us achieve this goal. Open the timeline in the Playground interface. Open the Playground interface and run the code, as shown in. During the program running, a line segment is drawn on the right timeline. the horizontal axis is the time experienced, and the vertical axis is the I value change, after the execution is complete, drag the line segment to view the I value in the running history. In this example, the longer the line segment in the timeline, the higher the execution efficiency.
2. do while statement
The use of the do while statement is similar to that of the while statement, but the do while statement is used to determine the structure of the loop condition afterwards. The statement format is as follows:
Do {
Statement Group
} While loop Condition
The do while loop does not have an initialization statement, and the number of cycles is unknown. no matter whether the loop conditions are met or not, the system first executes the loop body and then judges the loop conditions. If the conditions are met, the loop body is executed. If the conditions are not met, the loop is stopped.
The following shows the sample code:
var i:Int64 = 0do{ i++} while i * i < 100000println("i = \(i)")println("i * i = \(i * i)")
The output result is as follows:
I = 317.
I x I = 100489
This example is the same as the example in the previous section. It finds the maximum integer with the number of records less than 100 000. The output results are the same.
III. for statements
For statements are the most widely used and functional loop statements. The general format is as follows:
For initialization; loop condition; iteration {
Statement Group
}
When the program executes the for statement, it first executes the initialization Statement, which is used to initialize the cyclic variables and other variables. Then the program will check whether the cyclic conditions are met. If yes, then, execute the loop body and calculate the iteration statement, and then judge the loop condition again and again until the loop condition is determined to be invalid. The termination statement is generally used to change the cyclic conditions. It can operate on cyclic variables and other variables.
The following sample code is calculated from 1 ~ 9 square table program:
println("n n*n")println("---------")for var i = 1; i < 10; i++ { println("\(i) x \(i) = \(i * i)")}
The output result is as follows:
N * n
---------
1x1 = 1
2x2 = 4
3x3 = 9
4x4 = 16
5x5 = 25
6x6 = 36
7x7 = 49
8x8 = 64
9x9 = 81
At the beginning of the loop part of this program, I is assigned a value of 1 to the loop variable. Each loop must judge whether the I value is less than 10. If it is true, the loop body is executed, then add 1 to I. Therefore, the final result is to print 1 ~ 9 square, excluding 9.
Initialization, loop conditions, and iterations can all be empty statements (but the semicolon cannot be omitted). When the three statements are empty, they are equivalent 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 sequence of statements separated by commas, as shown in the following code:
var x:Int32var y:Int32for x = 0, y = 10; x < y; x++, y-- { println("(x,y) = (\(x),\(y))")}
The output result is as follows:
(X, y) = (0, 10)
(X, y) = (1, 9)
(X, y) = (2, 8)
(X, y) = (3, 7)
(X, y) = (4, 6)
4. for in statements
Swift provides a for loop specifically used to traverse a set-for in loop. To use a for in loop, you do not have to write code according to the standard for routine. You only need to provide a set to traverse the code.
Assume there is an array. The original way to traverse the array is as follows:
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 result is 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 preceding statement let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] declares and initializes a set of 10 element arrays, currently, you only need to know that when initializing an array, you need to put elements of the same type into […] And use commas to separate (,). We will introduce the array set in Chapter 10th.
The for in loop statement is used to traverse the array as follows:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];println("----for in----")for item in numbers { println("Count is: \(item)")}
The output result is 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
From the above example, we can find that item is a cyclic variable, and item is declared by var before it is declared by implicit variables. The in clause is followed by a set instance. The for in statement will extract the elements from the set one by one and save them to the item. It can be seen that the for in statement is much easier to traverse the set. But for other operations, the for in loop is not suitable.
For more information, please refer to the first domestic Swift book "Swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf
Welcome to Zhijie iOS public classroom Platform
How to Write a circular statement in C #
C # Many syntaxes are actually the same as C and C ++, so the While (), do while (), (), all these statements can be used in c.
While (I <100)
{
// Todo
I ++;
}
Do
{
I ++;
// Todo
}
While (I <100)
For (int I = 0; I <100; I ++)
{}
Hope to help the landlord
For Loop statements in programming
The for loop body is enclosed in braces. If there is only one statement, braces can be omitted.
For ()
For ()
{
Statement;
} Is equivalent
For ()
{()
{
Statement;
}
}
However:
For ()
For ()
{
Statement;
}
A ++;
It is different from
For ()
{
For ()
{
Statement;
}
A ++;
} The latter a ++ is in the first loop body. The former means that a ++ is not