Control flow in Swift- For loop.

Now we need to understand control flow in our applications. What if we have to count the no of people entering into the gates in a day, or  count the no of cans processed in one round by the machine. In such cases where we need to do a certain piece of work over & over again; like incrementing the counter to count no_of_people or incrementing the counter to determine the no_of_cans processed; we make use of the control statements.

Swift provides two kinds of loop that perform a set of statements a certain number of times:

  1. The forin loop (performs a set of statements for each item in a sequence)

  2. The for loop (performs a set of statements until a specific condition is met)

typically by incrementing a counter each time the loop ends.

For Loop:

The syntax is as follows.

the syntax has three parts.

  1. variable initialization (var i)
  2. variable condition (i<10)
  3. variable increment-or-decrement (i++/ i–)

It is not mandatory to use parentheses after for. But it is mandatory to use the curly braces {} after the increment-or-decrement condition. The curly parenthesis is use to indicate the starting and ending of the control statement body.

The loop is executed as follows:

  1. When the loop is first entered, the initialization expression is evaluated once, to set up any constants or variables that are needed for the loop.

  2. The condition expression is evaluated. If it evaluates to false, the loop ends, and code execution continues after the for loop’s closing brace (}). If the expression evaluates to true, code execution continues by executing the statements inside the braces.

  3. After all statements are executed, the increment expression is evaluated. It might increase or decrease the value of a counter, or set one of the initialized variables to a new value based on the outcome of the statements. After the increment expression has been evaluated, execution returns to step 2, and the condition expression is evaluated again.

For-In:

You use the forin loop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string. If you don’t know whats an array, string don’t worry we will talk about it in detail in the up coming articles. Lets take an example to understand this. Lets say we want to print the table for 2times. we can do it as follows.

the syntax for for in loop has 2 parts.

  1. variable to be iterated through a sequence. (index)
  2. range Operation. (1…10)

 Range Operation:

The range operation are of two types.

  1. inclusive of the last value in the range (1…10) [closed range operation]
  2. exclusive of the last value in the range (1..<10) [open range operation]

the output of the program will be

In the above example, index is a constant whose value is automatically set at the start of each iteration of the loop. It does not have to be declared before it is used. It is implicitly declared simply by its inclusion in the loop declaration, without the need for a let declaration keyword.

If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name as follows.

observe the for in loop in the above snippet. It does not take any value. Instead it uses ‘_’ .The underscore character _ (used in place of a loop variable) causes the individual values to be ignored and does not provide access to the current value during each iteration of the loop. Thus giving the following line as the answer.