Home>

The for loop is a loop control structure,You can effectively write a specific number of loops to execute.

grammar

The syntax of a for loop in the swift programming language is:

for init;condition;increment {
 statement (s)
}

Here is the flow control in a loop:

The initialization init step is executed first,And only once.At this step,You can declare and initialize any loop control variable. As long as a semicolon appears,You don't need to put a statement here.

Next, calculate the conditions.If true,The loop body is executed.If it is false,The loop body is not executed,Just in the for loop flow control jumps to the next statement.

After the for loop executes,Control flows back to incremental declarations.This statement can update any loop control variable.This statement can be left blank,As long as a semicolon appears after the condition.

Conditions are now recalculated. if it is ture,The process of loop execution repeats (the body of the loop, then increments,Then the conditions). When the condition is false,The cycle is terminated.

flow chart

Example

import cocoa
</p>
<p>
var someints:[int]=[11, 22, 33]
</p>
<p>
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 11
value of someints [1] is 22
value of someints [2] is 33

for-in loopor-in loop iterative project,Such as numeric ranges,Items in an array,Or the character set in the string:

grammar

For-in loop syntax in swift programming language:

for index in var {
 statement (s)
}

flow chart

Example

import cocoa
</p>
<p>
var someints:[int]=[11, 22, 33]
</p>
<p>
for item in someints {
 println ("value of index is \ (item)")
}

When the above code is executed,It produces the following results:

value of index is 11
value of index is 22
value of index is 33
  • Previous C # Cinema Ticketing System Graduation Design (1)
  • Next Introduction to the use of UISwitch buttons in iOS development