Explain how to write a For loop in Swift programming _swift

Source: Internet
Author: User

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

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.