The while loop structure of the Swift language _swift

Source: Internet
Author: User


The while loop statement in the Swift programming language repeats a target statement as long as the given condition is true.



Grammar
The syntax for the while loop of the Swift programming language is:


Copy Code code as follows:

While condition
{
Statement (s)
}

Here statement (s) can be a single statement or block of statements. Condition can be any expression. The loop iteration condition (condition) is true. When the condition is false, the program is programmed to enter the line immediately after the loop.





Number 0, string "0" and "", Empty list (), and undef are all fake in a Boolean context, except that all other values are true. Negative sentence a truth value! Or not, returns a special false value.



Flow chart






While loop here, the key point: the loop may never run. When the test condition and the result are false, the loop body skips the while loop, and the first statement is executed.



Example


Copy Code code as follows:

Import Cocoa

var index = 10


While index < 20
{
println ("Value of index is \ (index)")
index = index + 1
}


Here we use the comparison operator < to compare the 20 variable index values. Therefore, although the value of the index is less than the next-generation code of the code block that the 20,while loop continues to execute, and the exponent value is superimposed to 20, this exits the loop. At execution time, the above code produces the following results:




Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19


Do...while Cycle
unlike for and while loops, test the loop condition at the top of the loop, do...while the loop to check its state at the bottom of the loop.



Do ... while loops are similar to a while loop, except that the Do...while loop guarantees execution at least once.



Grammar
The do...while syntax in the Swift programming language is as follows:


Copy Code code as follows:

Todo
{
statement (s);
}while (condition);

It should be noted that the conditional expression appears at the bottom of the loop, so the loop statement executes once before the condition is tested. If the condition is true, the control stream jumps back up and continues execution, and the loop statement executes again. Repeat this process until the given condition is false.





Number 0, string "0" and "", Empty list (), and undef are all fake in a Boolean context, except that all other values are true. Negative sentence a truth value! Or not, returns a special false value.



Flow chart






Instance


Copy Code code as follows:

Import Cocoa

var index = 10


do{
println ("Value of index is \ (index)")
index = index + 1
}while Index < 20


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




Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19




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.