How to Loop Again in for Loop in Swift
In programming, loops are used to repeat a block of lawmaking. For example, if you want to show a message 100 times, then you lot can use a loop. Information technology's just a simple case, you tin achieve much more than with loops.
In the previous tutorial, y'all learned well-nigh the Swift for-in Loop. Here, you are going to learn almost while and repeat...while loops.
Swift while Loop
Swift while loop is used to run a specific code until a certain condition is met.
The syntax of while loop is:
while (condition){ // body of loop } Here,
- A
whileloop evaluatesconditioninside the parenthesis(). - If
conditionevaluates totrue, the code within thewhileloop is executed. -
conditionis evaluated once again. - This process continues until the
statusisfalse. - When
conditionevaluates tofaux, the loop stops.
Flowchart of while Loop
Example ane: Swift while Loop
// program to display numbers from ane to 5 // initialize the variable var i = one, north = 5 // while loop from i = one to 5 while (i <= n) { print(i) i = i + 1 } Output
1 2 3 iv five
Here'due south how the plan works.
| Variable | Condition: i <= northward | Action |
|---|---|---|
i = 1 due north = 5 | truthful | 1 is printed. i is increased to 2. |
i = 2 n = 5 | truthful | 2 is printed. i is increased to 3. |
i = iii n = 5 | truthful | iii is printed. i is increased to 4. |
i = 4 n = 5 | true | iv is printed. i is increased to 5. |
i = 5 n = five | true | five is printed. i is increased to 6. |
i = 6 n = 5 | fake | The loop is terminated. |
Example 2: while Loop to Brandish Game Level
var currentLevel:Int = 0, finalLevel:Int = 5 let gameCompleted = true while (currentLevel <= finalLevel) { if gameCompleted { print("You have passed level \(currentLevel)") currentLevel += 1 } } impress("Level Ends") Output
Y'all have passed level 0 You have passed level 1 Yous take passed level two You take passed level three Yous accept passed level 4 You take passed level 5 Level Ends
In the to a higher place example, we have used a while loop to cheque the current level and display it on the panel.
repeat...while Loop
The echo...while loop is like to while loop with one central difference. The body of repeat...while loop is executed one time before the test expression is checked.
The syntax of repeat..while loop is:
repeat { // trunk of loop } while (condition) Here,
- The body of the loop is executed at get-go. Then
conditionis evaluated. - If
conditionevaluates totrue, the body of the loop inside therepeatstatement is executed again. -
statusis evaluated in one case once again. - This process continues until
conditionevaluates tofalse. And so the loop stops.
Flowchart of repeat...while Loop
Case three: repeat...while Loop
// program to display numbers var i = i, north = 5 // echo...while loop from 1 to 5 echo { print(i) i = i + 1 } while (i <= n) In the above instance, initially the value of i = 1 and n = 5. For the first time the statement inside the loop executes without checking the status. The condition is checked after the first iteration is consummate.
Hither's how this programme works,
| Variable | Status: i <= n | Activeness |
|---|---|---|
i = 1 northward = five | non checked | 1 is printed. i is increased to 2. |
i = 2 north = 5 | true | 2 is printed. i is increased to 3. |
i = iii n = 5 | true | 3 is printed. i is increased to 4. |
i = 4 n = v | true | 4 is printed. i is increased to 5. |
i = five north = 5 | true | 5 is printed. i is increased to 6. |
i = half dozen north = 5 | faux | The loop is terminated. |
Infinite while Loop
If the condition of a while loop is always true, the loop runs for infinite times (until the retentivity is full). This is called infinite while loop. For example,
while (true) { print("Endless Loop") } Output
Endless Loop Endless Loop . . .
Here, the status is always true. Hence, the while loop will run for space times.
for vs while loop
A for-in loop is ordinarily used when the number of iterations is known. For example,
// this loop is iterated 5 times for number in i...5 { // trunk of loop } Nevertheless, a while loop is ordinarily used when the number of iterations are unknown. For example,
while (status) { // body of loop } Notation: The working of echo...while is the same as the while. Hence, information technology is besides used when the number of iterations is unknown.
Source: https://www.programiz.com/swift-programming/repeat-while-loop
0 Response to "How to Loop Again in for Loop in Swift"
Post a Comment