Decision Making And Looping Statement In Swift Programming Language

Introduction

If you have any programming experience, then you must have used loops. In looping,a sequence of statements is executed untill some conditions for the termination of the loops are satisfied. A looping process consists of four steps.
  • Setting and initializing a counter.
  • Executing the statements in the loops.
  • Texting a specified condition for the execution of the loop.
  • Incrementation or decrementation of the counter.
Looping types 
  • While
  •  do While
  •  for
  •  for each
While Statement 

A While statement conditionally executes a statement zero and more times; the values are executed to display.

Example program
  1. var index = 10  
  2. while index < 20  
  3. {  
  4.     print("value of index is\(index)")  
  5.     index = index + 1  
  6. }  
output
  1. value of index is 10
  2. value of index is 11
  3. value of index is 12
  4. value of index is 13
  5. value of index is 14
  6. value of index is 15
  7. value of index is 16
  8. value of index is 17
  9. value of index is 18
  10. value of index is 19
do While statement

Use a While statement to find the first occurrence of a value in an array.

Do Statement

A do statement conditionally executes a statement one or more times.

Example program
  1. var index = 10  
  2. do {  
  3.     println("value of index is \(index)")  
  4.     index = index + 1  
  5. }  
  6. while index < 20  
Output 
  1. value of index is 10
  2. value of index is 11
  3. value of index is 12
  4. value of index is 13
  5. value of index is 14
  6. value of index is 15
  7. value of index is 16
  8. value of index is 17
  9. value of index is 18
  10. value of index is 19
for statement

 A "for" statement evaluates a sequence of initialization expressions and then, while a condition is true; it repeatedly executes a statement and evaluates a sequence of iteration expressions.

Example program 
  1. var some Ints: [Int] = [10, 20, 30]  
  2. for {  
  3.     print("value of index is \(index)")  
  4. }  
Output 
  1. value of index is 10
  2. value of index is 20
  3. value of index is 30
This blog is presented to you to help you  learn the basic concepts of decision making and loops statements in Swift programming language.
Ebook Download
View all
Learn
View all