Swift While Loop

While loop are used to iterates block of statement under terminate condition. This loop are executed when terminated are not invalid.


//set initial point
var fines=0

//Executed the statement until of satisfies 
//Given condition is not (fines not greater than 200).
while fines <= 200{
  print("Fines are \(fines)")
  fines+=50 //Modified terminating condition
}

print("After While loop Fines :\(fines)")

Output

Fines are 0
Fines are 50
Fines are 100
Fines are 150
Fines are 200
After While loop Fines :250

When if we are need to execute block of statement minimum at least once. Then we can use alternative from of while loop.

//set initial point
var fines=250

repeat{
  print("Fines are \(fines)")
  fines+=50 //Modified terminating condition
}while fines <= 200


print("After While loop Fines :\(fines)")

Output

Fines are 250
After While loop Fines :300

That is similar to do-while loop in c, c++ and java programming.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved