Swift While cycle


Release date:2023-11-28 Update date:2023-12-11 Editor:admin View counts:143

Label:

Swift While cycle

Swift while the loop begins by calculating a single condition. If the condition is true will run a series of statements repeatedly until the condition changes to false .

Grammar

Swift while the syntax format of the loop is as follows:

while condition
{
   statement(s)
}

In grammar statement(s) can be a statement or a block of statements. condition can be an expression. If the condition is true``will run a series of statements repeatedly until the condition changes to  ``false .

The number 0, the strings '0' and '' , empty list() and undefined variables are false and the rest are true . true use it backwards! Number or not , take the reverse and return false .

Flow chart:

Image0

Example

import Cocoa

var index = 10

while index < 20
{
   print( "The value of index is \(index)")
   index = index + 1
}

The output of the above program execution is as follows:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).