Swift Programming - Zero To Hero - Part Two

Introduction

This is part two of the Swift Programming - Zero to Hero series. In this article we will see loops in Swift. Please follow my preious article before proceeding further.

Loop in Swift

Loops are used to iterate data from an array, range of numbers, or character from a string. Consider a situation in your program where you have to execute the same set of code ten times. In such a situation, instead of writing the same code ten times, define the code in a Loop and execute it ten times. This saves the complexity and time involved in coding. The following are the Looping statements mainly used in Swift.

  1. for
  2. for-in
  3. while
  4. do...while

For loop

This is the traditional method of looping that executes a sequence of statements multiple times and abbreviates the code that manages the Loop variables.The syntax of For loop consists of initializer, condition, and loop expression. Initializer initializes a variable; the Loop expression increments or decrements the value of the variable; and the condition refers to a condition specified in the loop.

Syntax

  1. for (init;condition;loop expression){  
  2.    statements  
  3. }  

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.

The condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the loop expression statement. This statement allows you to update any loop control variables.

 For -in loop

The for-in loop iterates over collections of items, such as ranges of numbers, items in an array, or characters in a string. The working of for-in loop is similar to for loop. The statements in for-in loop continue to execute for each element in a collection. After iterating each elements in collection, the control transfers to next block of code.

Syntax

  1. for index in var {  
  2.    statement(s)  
  3. }  

While

We can use while loop to execute a statement until specified condition evaluates to false. The best scenario to use while loop is when we don’t know how many times the loop should be executed. If the Condition in While loop is true, then the statements inside while loop are executed. Otherwise the statements are not executed.

Syntax

  1. while condition {  
  2.    statement(s)  
  3. }  

do… while

The function of do…while loop is similar to the while loop. The main difference in do..while loop is that the condition is checked at the end of the loop. This means the loop executes at least once even if the condition is false.

Syntax

  1. do {  
  2.    statement(s);  
  3. }while( condition );  

Conclusion

In this article we learned about looping statements, I hope it was helpful. In my next article, we will see Arrays and accessing Array elements using Looping Statements and Conditional Statement.


Similar Articles