Home » Swift for-in loop

Swift for-in loop

by Online Tutorials Library

Swift For-in Loop

The Swift For-in loop is used to run a set of tasks for a certain number of times. This loop does repetitive process a number of times.

For example

If you want to write a syntax that prints “Hello tutoraspire”, the simple syntax will be:

What would be if you have to write the same print statement 5 times? The simple solution would be:

But, there is one solution. You can use for-in loop to print the same statement and make the code shorter.

Example 1:

Output:

Good Morning Ajeet  Good Morning Aryan  Good Morning Ayan  Good Morning Alex  

A Swift program to print the table of a specific number (Here, it is 5.)

Output:

1 times 5 is 5  2 times 5 is 10  3 times 5 is 15  4 times 5 is 20  5 times 5 is 25  6 times 5 is 30  7 times 5 is 35  8 times 5 is 40  9 times 5 is 45  10 times 5 is 50  

Next TopicSwift While Loop

You may also like