YouTip LogoYouTip

Swift Repeat While Loop

# Swift repeat...while Loop [![Image 4: Swift Loops](#)Swift Loops](#) Unlike for and while loops, which evaluate the condition statement before executing the loop body, the Swift repeat...while loop checks the condition at the end of the loop execution. ### Syntax The syntax format of the Swift repeat...while loop is as follows: ```swift repeat { statement(s); }while( condition ); Please note that the conditional expression appears at the end of the loop, so the statement(s) in the loop will be executed at least once before the condition is tested. If the condition is true, the control flow jumps back to repeat, and then re-executes the statement(s) in the loop. This process repeats continuously until the given condition becomes false. The number 0, the string '0' and "", an empty list(), and undefined variables are all **false**, while everything else is **true**. true is negated using the **!** sign or **not**, and returns false after negation. **Flowchart:** !(#) ### Example ```swift import Cocoa var index = 15 repeat{ print( "index value is (index)") index = index + 1 }while index < 20 The output of the above program execution is: ```text index value is 15 index value is 16 index value is 17 index value is 18 index value is 19 [![Image 6: Swift Loops](#)Swift Loops](#)
← Swift Continue StatementSwift While Loop β†’