Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A 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.
while condition {
    // statements to execute
}
Execution Mechanics:
  1. The condition expression is evaluated.
  2. If the condition evaluates to true, the statements within the loop body are executed.
  3. Upon reaching the closing brace, control flow returns to step 1.
  4. If the condition evaluates to false, the loop terminates immediately, and execution resumes at the first statement following the loop’s closing brace.
Because the condition is evaluated before the loop body executes, it is possible for the statements inside a standard 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.
repeat {
    // statements to execute
} while condition
Execution Mechanics:
  1. The statements within the repeat block are executed unconditionally.
  2. The condition expression is evaluated.
  3. If the condition evaluates to true, control flow jumps back to step 1.
  4. If the condition evaluates to false, the loop terminates.
This structure guarantees that the loop body will execute at least once, regardless of the condition’s initial state.

Condition Evaluation and Compound Conditions

Swift enforces strict type safety for loop conditions. A standard condition expression must evaluate explicitly to a Bool 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.
while conditionA, let value = optionalExpression, value > 0 {
    // Executes as long as conditionA is true, optionalExpression is not nil, 
    // and the unwrapped value is greater than 0.
}

Optional Binding and Pattern Matching

Swift extends the while 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.
while let unwrappedValue = optionalExpression {
    // statements to execute using 'unwrappedValue'
}
Pattern Matching: Iterates as long as the evaluated expression matches a specific pattern, such as an enumeration case with associated values.
while case .someCase(let associatedValue) = enumExpression {
    // statements to execute using 'associatedValue'
}

Control Transfer Statements

Execution within a while 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 or switch statement. No further iterations occur, the condition is not re-evaluated, and control flow transfers to the code immediately following the terminated construct.
while conditionA {
    if conditionB {
        continue // Skips to the next evaluation of conditionA
    }
    
    if conditionC {
        break // Exits the loop entirely
    }
}

Labeled Statements

When working with nested loops or switch 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.
outerLoop: while conditionA {
    switch someValue {
    case matchA:
        break // Terminates the switch statement only; the while loop continues
    case matchB:
        break outerLoop // Terminates the while loop entirely
    default:
        continue outerLoop // Skips to the next iteration of the while loop
    }
}
Master Swift with Deep Grasping Methodology!Learn More