EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials Swift Tutorial Swift do-while

Swift do-while

Anusua Dutta
Article byAnusua Dutta
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated April 11, 2023

Swift do-while

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.

ADVERTISEMENT
Popular Course in this category
SWIFT Course Bundle - 21 Courses in 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

Swift do-while

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 :

Swift do-while 1

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:

Swift do-while 2

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:

Swift do-while 3

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 4

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:

Swift do-while 4

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:

example 6

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.

  1. Swift Dictionary
  2. Swift map
  3. Swift forms
  4. Swift For Loop
ADVERTISEMENT
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW