Control Flow : Swift tutorial

Swift Provides diffrent type of controll flow statements like:

  • Loops Statements : Loops are used for perform the task multiple times or for handel the arays, dictionaries, string etc
  • Conditional Statements : Execute some line of code at certain condition.
  • Control Transfer Statements : Change the order in which your code is executed, by transferring control from one piece of code to another.

Loops Statements

  • Loops are used for execute Number of line of code multiple times/statement is execute sequentially. This execution is depending on condtions is true/false.
  • Swift have these loopes statement
    • For-In loops
    • While loop
    • Repeat-While loop
1. For-In loop

For-In loop is used when we need to execute set of code at certain number of time in repeated manner.
For-In loop in swift syntax are following :

  • Uses a for-in loop to iterate over the items in an array :

  • Uses Half upper Range operator (..<) to include lowwer bound but not upper bound. By this operator we can run for loop at perticular range. We can follow this with other range operators also like “One-Sided Ranges(...14...)”, “Closed Range Operator(1...5)”

  • 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.

  • use with stride(from:to:by:), Its use for a constant gap between in sequence. And it return the sequence starting value but not end value.

  • We can also use For-In loop with dictionary. Each item of dictionary is getting in (key, value)pair.

2. While loop

while loop performs a set of statements until a condition becomes false. While loop is used when we not know the number of Running of set of statement before first execution begin.
While loop in swift syntax are following :

while loop starts by evaluating a single condition. If the condition is true, a set of statements is repeated until the condition becomes false.

3. repeat-while loop

The other variation of the while loop, known as the repeat-while loop, performs a single pass through the loop block first, before considering the loop’s condition. It then continues to repeat the loop until the condition is false.
repeat-while loop in swift syntax are following :

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.