ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
while loop is a control flow statement that repeatedly executes a block of code as long as a specified boolean condition evaluates to true. Swift provides two variants of this construct: the standard while loop, which evaluates its condition prior to each iteration, and the repeat-while loop, which evaluates its condition after each iteration.
Standard while Loop
The standard while loop evaluates its condition at the start of each pass through the loop.
- The
conditionexpression is evaluated. - If the condition evaluates to
true, the statements within the loop body are executed. - Upon reaching the closing brace, control flow returns to step 1.
- If the condition evaluates to
false, the loop terminates immediately, and execution resumes at the first statement following the loop’s closing brace.
while loop to execute zero times if the initial condition is false.
repeat-while Loop
The repeat-while loop (analogous to the do-while loop in C-based languages) performs a single pass through the loop block before evaluating its condition.
- The statements within the
repeatblock are executed unconditionally. - The
conditionexpression is evaluated. - If the condition evaluates to
true, control flow jumps back to step 1. - If the condition evaluates to
false, the loop terminates.
Condition Evaluation and Compound Conditions
Swift enforces strict type safety for loop conditions. A standard condition expression must evaluate explicitly to aBool type. Attempting to use non-boolean values, such as integers (e.g., while 1), will result in a compile-time error.
Swift also supports compound condition lists. A while loop can evaluate multiple conditions separated by commas. These conditions evaluate sequentially from left to right and short-circuit; if any condition evaluates to false or nil, the entire condition list evaluates to false, and the loop terminates immediately without evaluating the remaining conditions.
Optional Binding and Pattern Matching
Swift extends thewhile loop with optional binding (while let and while var) and pattern matching (while case), allowing loops to iterate based on the presence or structure of data.
Optional Binding: Iterates continuously as long as an optional expression returns a non-nil value. The value is unwrapped and assigned to a local constant or variable for the duration of the loop iteration.
Control Transfer Statements
Execution within awhile or repeat-while loop can be manually altered using control transfer statements:
continue: Immediately halts the current iteration, skipping any remaining statements in the loop body. Control flow is transferred directly to the condition evaluation to determine if the next iteration should begin.break: Immediately terminates the innermost enclosing loop orswitchstatement. No further iterations occur, the condition is not re-evaluated, and control flow transfers to the code immediately following the terminated construct.
Labeled Statements
When working with nested loops orswitch statements within loops, Swift allows the use of statement labels to explicitly define which construct a break or continue statement targets.
Because an unlabeled break terminates the innermost enclosing loop or switch statement, using an unlabeled break inside a switch that is nested within a while loop will only terminate the switch. A labeled break is strictly required to exit the enclosing while loop from within the nested switch.
Master Swift with Deep Grasping Methodology!Learn More





