Updated April 19, 2023
Introduction to Swift while loop
Swift while loop is a loop that is used for performing a condition check and its loop code is also for making the loop work in a repeated fashion until the condition within the loop gets failed. Swift while loop is quite advantageous when the number of iterations is not known before the first iteration gets started for traversal. Swift while loop is also of two types where the first type is the general while loop used for evaluating its condition at each pass and another while loop which is a repeat-while loop for evaluating its condition at the end of each pass within the loop.
Syntax:
The syntax for general while loop where the evaluation of condition starts from the beginning of the loop is:
while(condition_for_execution)
{
Statements_for_body
}
Syntax for a repeat while loop, where the evaluation of condition starts at the end of the loop, is:
repeat
{
Statements_for_body
} while(condition_for_execution)
Flowchart
Given below are the flowcharts:
1. While loop flowchart.
The flowchart for the while loop depicts the flow where the test condition gets evaluated at the start and if the condition gets satisfies to true then the next set of statements within the loop and body will get executed if not which means if the condition evaluates to false then the while loop will get excited and will not get continued with the set of statements in the entire flowchart.
2. Repeat while loop flowchart.
Repeat while loop flowchart flows in a way where the statement of the body for while loop gets executed only if the test condition is checked once, if the conditional check comes out to be true then statements inside the body gets executed one by one and then the test expression gets evaluated. The process runs until the condition is evaluated else it is not true means evaluated to false and the loop gets terminated.
How while loop Works in Swift?
- The working of while loop in swift gets depicted easily using flowchart but the real flow has a lot to do as everything depends on the execution of the statements. The flow depends on the number of statements and the number of conditions it goes under verification while executing of the code in an incremental manner.
- While loop in swift works in a way where it will first check for the test condition which is mostly a Boolean expression and then it will be evaluated whether true or false, suppose in case the evaluation comes out to be true then it will check for the statements inside the while loop at the time of execution followed by the test condition. It gets evaluated till the time the test condition gets evaluated to false. At last, when the test condition evaluates to false it will get terminated.
- Something similar happens with repeat while loop with very little difference in the behavior of flow in comparison to the while loop in swift where the execution gets evaluated in a reciprocated manner as that of while loop. Here the loop evaluates the condition at each pass made to the control flow. The repeat while loop has one more advantage like it helps in making the while loop executed and verified at least once before the entire test execution takes place and expression is checked.
- Only after the first execution, the test condition gets evaluated to true, and then finally all the statements inside the body loop get executed and evaluated again. This entire process gets executed and evaluated to false once the loop gets terminated at last.
- There are times and scenarios the entire while loop starts to execute in an infinite manner and comes to halt. It also helps in making the entire while loop getting executed till the value of false and is executed an infinite number of times.
Examples of Swift while loop
Given below are the examples of Swift while loop:
Example #1
This program demonstrates the while loop where the comparison operator is used for verifying the flow where the index flow is checked against value 30 until the value is till 30 all the values will get executed otherwise it will not print the other values as shown in the output.
Code:
import Foundation
import Glibc
var indx_1 = 15
while indx_1 < 30
{
print( "index_value of \(indx_1)")
indx_1 = indx_1 + 1
}
Output:
Example #2
This program demonstrates the swift repeat while loop which is used for representing the sum with some value which is used for printing the values once the condition for execution comes out to be true and will print all the values as shown in the output.
Code:
import Foundation
import Glibc
var sm_0 = 4
repeat
{
print(sm_0)
sm_0 = sm_0 + 2
}
while sm_0 < 20
Output:
Example #3
This program demonstrates the while loop where the value is taken gets decremented by 1 when while loop expression is greater than or equal to 0 as shown in the output.
Code:
import Foundation
import Glibc
var i_o = 8
while i_o >= 0
{
print(i_o)
i_o -= 1
}
Output:
Example #4
This program demonstrates the while loop with the break statement wherever a third even number is encountered as per manipulation as shown in the output.
Code:
import Foundation
import Glibc
var k = 8
var evn_ct = 0
while true {
if k % 2 == 0
{
print("Even_no")
evn_ct += 3
if evn_ct == 9
{
break
}
}
print(k)
k += 2
}
Output:
Example #5
This program demonstrates the infinite loop for the swift while loop which may occur once the program doesn’t get terminated properly according to the conditional statements and the body of the code execution will give an infinite loop.
Code:
import Foundation
import Glibc
while (true)
{
print("Welcome_all!")
}
Output:
Conclusion
Swift while loop plays a pivotal role with respect to other loops when it comes to performing a repetitive action on the iteration. It makes the process of traversal and conditional checks with the number of statements easy and compatible with other conditional checks. It also helps in easy termination if the Boolean statement gets evaluated to true or false based on conditions and body of the while loop.
Recommended Articles
We hope that this EDUCBA information on “Swift while loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.