Home>

A while loop statement in the swift programming language, as long as the given condition is true,Repeat a target statement.

grammar

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

while condition
{
 statement (s)
}

Here statement (s) can be a single statement or a block of statements.condition can be any expression.The loop iterates when the condition is true. When the condition is false,Program control then advances to the line immediately after the loop.

The number 0, the strings "0" and "", the empty list list (), and undef are all false in a boolean context,All other values ​​are true. Negative sentence is a truth! Or not returns a special false value.

flow chart

while loop is here,The key point:the loop may never run.When the test conditions and results are false,The loop body will skip the while loop and the first statement after it will be executed.

Example

import cocoa
var index=10
</p>
<p>
while index<20
{
 println ("value of index is \ (index)")
 index=index + 1
}

Here, we are using the comparison operator<to compare 20 variable index values.So, although the value of the index is less than 20, the next code in the while loop continues to execute,And add the value of the index to 20, here exit the loop.When executed,The above code will produce 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 loopUnlike for and while loops, test loop conditions at the top of the loop,The do ... while loop checks that its status is at the bottom of the loop.

The do ... while loop is similar to the while loop, except that the do ... while loop is guaranteed to execute at least once.

grammar

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

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

It should be noted thatConditional expressions appear at the bottom of the loop,So the loop statement is executed once before testing the condition.If the condition is true,Control flow jumps back and execution continues,The loop statement is executed again.Repeat the process,Until the given condition is false.

The number 0, the strings "0" and "", the empty list list (), and undef are all false in a boolean context,All other values ​​are true. Negative sentence is a truth! Or not returns a special false value.

flow chart

Examples

import cocoa
var index=10
</p>
<p>
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
  • Previous Introduction to the use of UISwitch buttons in iOS development
  • Next Analysis of SQL data manipulation statements