for Loops

Swift also supports the classic for loop:

for initialization; condition; increment {
    // Code to execute at each iteration
}

Semicolons separate the three parts of the for loop. Each part performs a specific function in the three steps of the loop’s execution:

  1. When the loop is entered, the initialization expression is evaluated to set up the iterator for the loop.

  2. The condition expression is evaluated. If it is false, the loop is ended and execution of the code is transferred to after the loop. If it is true, then the code inside the loop’s braces ({}) is executed.

  3. After the code between the braces is executed, the increment expression is executed. Depending on the code, the incrementer can be increased or ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.