Swift for loop

Swift for loop allows some code to be executed again and again.
 

Syntax of for loop

for initialization;conditional expression;update counter {
  // Statements to be executed
}

 
The loop executes as follows :

Step 1: When the loop is first entered, the initialization statement is evaluated.
Step 2: The conditional is evaluated; if the conditional evaluates to false, the loop ends and the block is not executed and the program continues after the closing curly brace. If the conditional expression evaluated to true, then the block is executed.
Step 3: After the block has executed the update counter part is evaluated. After that the execution of the program goes back to step 2 and the condition expression is evaluated again.
 

for loop example in Swift

The following for loop executes the print statement 5 times:
 
Swift for loop

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags