A For loop is a loop control structure that can be effectively written to perform a specific number of loops.
Grammar
The syntax for the For loop in the Swift programming language is:
Copy Code code as follows:
For Init; Condition increment{
Statement (s)
}
The following is the process control in a loop:
Initializing the init step is performed first and only once. In this step, you can declare and initialize any of the loop control variables. As long as a semicolon appears, there is no need to put a statement here.
Next, calculate the condition. If true, the loop body is executed. If it is false, the loop body does not execute, just the for loop flow control jumps to the next statement.
After the For loop executes, the control process transfer picks up to the increment declaration. This statement can update any loop control variable. This statement can be left blank, as long as a semicolon appears after the condition.
The condition is now recalculated. If it is true, the loop-executing process repeats (the loop body, then increments, then the condition). When the condition is false, the loop terminates.
Flow chart
Example
Copy Code code as follows:
Import Cocoa
var someints:[int] = [11, 22, 33]
for var index = 0; Index < 3; ++index {
println ("Value of Someints[\ (index)] is \ (Someints[index])")
}
When the above code is executed, it produces the following results:
Value of Someints[0] is one value of someints[1] is value of a
someints[2] is 33
For-in Cycle
or-in loop iteration items, such as a range of numbers, an item in an array, or a character set in a string:
Grammar
For-in Loop in the Swift programming language syntax:
Copy Code code as follows:
For index in Var {
Statement (s)
}
Flow chart
Example
Copy Code code as follows:
Import Cocoa
var someints:[int] = [11, 22, 33]
For item in Someints {
println ("Value of index is \" (item))
}
When the above code is executed, it produces the following results:
The value of index is one value of index is value of the
index is 33