Updated April 11, 2023
Definition of Swift do-while loop
Swift do-while loop is used for testing and checking the condition at the end of the loop with an exception that the do-while loop gets executed at least once before making other conditional checks. Swift do while loop behaves differently to other for and while loop in a way that in while loop the execution happens from top of the loop. Since in do-while loop the conditional expression lies in the end of the loop therefore all the statements within the loop should get executed before the conditional check and is also called repeat do-while.
Syntax:
The syntax flow is in a way where the do loop will perform the activity and then the entire body will get executed with the statement in the body of the program providing the conditional check as well which is present at the end of the loop as shown.
do
{
Statement_for_body;
}while( conditional_check );
Flowchart
The Flow of the code is in a way where the conditional check is made then the body of the code is executed in order to get the value as true the conditional check at the end or bottom should be satisfied if in case the bottom conditional check comes out to be false then the entire body of the code gets terminated.
How do While loop works in Swift?
- While loop in Swift works and flows in the manner which is represented in the flowchart above.
- The conditional expression that appears at the bottom should get executed once before the condition is verified and tested.
- If in the scenario the condition appears to be true then the control flow jumps back to the do loop and all the statements that are present within the loop gets executed seamlessly again.
- The iteration of the execution of the statements gets executed repeatedly until the condition becomes false.
- Once the iteration becomes false it will make the conditional number 0 and then “”, list as empty() and undef() all values are false, with most of the boolean format.
- Swift while loop is often called as Repeat while loop and there is not much difference to it. The main emphasis of repeat while loop is the conditional check point which gets executed once all the statements get executed.
- There is a situation when repeat while when not placed with proper statement and condition check might lead to infinite while loop. When the repeat loop statement is used the conditional check at the end gets checked and then all the statements within the loop gets executed.
- Whenever the situation requires to solve and reuse the set of code it is preferable to use swift do while loop with which all the iterations and statements will get resolved properly.
- Repeat while loop has some difference with while loop with the fact of the exception of the fact that while loop has conditional check at the top whereas repeat while loop compliments the fact as conditional check is present at the bottom and then the statements within the loop gets executed.
- Do while loop in Swift if evaluated to false initially will never execute any of the statements within it rather it will throw warning saying will never get executed and it will not get evaluated to false as well.
Examples of Swift do-while
Let us discuss examples of Swift do-while.
Example #1
This program demonstrates the repeat while loop where the game is initialized with the first stage and then the game is finished from the last stage following the conditional check and then the game is completed with the result as shown in the output.
Code:
import Foundation
import Glibc
var frst_stage:Int = 0, lst_stage:Int = 8
let finishd_game = true
repeat
{
if finishd_game
{
print("Completed_frst_stage: \(frst_stage)")
frst_stage += 1
}
} while (frst_stage <= lst_stage)
print("Excution_outside_while_loop.")
Output :
Example #2
This program demonstrates the conditional check which is present at the bottom of the code and gets executed with all the statements included within the loop until the condition becomes false as shown in the output with all the conditions and satisfactions.
Code:
import Foundation
import Glibc
var sm_0 = 4
repeat
{
print(sm_0)
sm_0 = sm_0 + 4
} while sm_0 < 20
Output:
Example #3
This program demonstrates the while loop which is different than repeat while loop in which the while conditional check appears at top not at the bottom of the code as shown in the output below.
Code:
import Foundation
import Glibc
var j_m = 2
while j_m < 36
{
print(j_m)
j_m = j_m + 6
}
Output:
Example #4
This program demonstrates the repeat while loop where the boolean expression being mentioned in the conditional check becomes false as shown in the output.
Code:
import Foundation
import Glibc
var v_1 = 10
repeat
{
print("v_1 : \(v_1)")
v_1 = v_1 + 5
} while( v_1<18 )
print("other_statements_working.")
Output:
Example #5
This program demonstrates the condition where using while loop with false will throw the exception as shown in the output saying that the false condition is always false and the loop will never get executed as shown in the output.
Code:
import Foundation
import Glibc
while false
{
print("conditional_false_norm")
}
print("if_condition_true_becomes.")
Output:
Example #6
The program demonstrated in Example 5 can be resolved and executed with the following set of code because the final code gets executed and on top of it the conditional check is done which is performed and mentioned at the bottom of the code as shown in the output.
Code:
import Foundation
import Glibc
repeat
{
print("conditional_true_becomes_executed_successfully.")
} while false
Output:
Conclusion
Swift do while loop is used for reusing the code with conditional checks. It also helps in making the entire code and statements execute with a flow and till the condition becomes false. It also helps in making the loops flexible and versatile to perform and adopt all the repetitive statements within a loop.
Recommended Articles
We hope that this EDUCBA information on “Swift do-while” was beneficial to you. You can view EDUCBA’s recommended articles for more information.