TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Repeat While Loop

Use the repeat-while loop to continue iterating until a condition is reached. Specify the break keyword.
Repeat. A dog barks in the night. It is on a repeat loop. Some things in Swift have no known end point—we stop them (break the loop) on demand.
Two keywords. The repeat loop requires a "repeat" and a "while" keyword. To have an infinitely repeating loop, we can use a "while true" part at the end.
First example. Here we have a repeat-while loop. We have a local variable named "i" and we increment this in the body of the repeat-while loop.

Note: The loop ends when our variable reaches the value 6. We print the statement "I have N cats" on each iteration.

Swift program that uses repeat loop var i = 0 // Use repeat loop. // ... No initial condition is checked. repeat { print("I have \(i) cats.") i += 1 } while i < 5 Output I have 0 cats. I have 1 cats. I have 2 cats. I have 3 cats. I have 4 cats.
Repeat, random numbers. Here is a simple program that repeats a function call. It terminates the repeat-while loop only when the function call returns a specific number.

Rand: This returns a random number. In the following if-statement, we break if the number is even (not odd).

Random

True: Consider using a "while-true" loop when an infinite (or indeterminate) loop is needed. A break can stop the loop.

Swift program that uses repeat loop, random numbers import Foundation // Initialize a seed number. sranddev() // Get random number in repeat loop. // ... Print number. // If number is even then break the loop. repeat { let number = arc4random() print(number) // Check for even number. if number % 2 == 0 { print("Even number, stopping loop.") break } } while true Output 546090829 4220801392 Even number, stopping loop.
Repeat, example 3. The repeat-while loop does not check its condition before the loop body is entered. So a variable (like x in this program) can have any value.
Swift program that shows no initial check, repeat-while var x = 999 repeat { // This block is entered on any value of x. // ... The value is not checked before the block is executed. print(x) } while x < 10 Output 999
A summary. The repeat-while loop is the same as a "do-while" loop in C-like languages. The word "repeat" may be easier to read for programmers. Its while part is required.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf