Loop in Swift programming Language

Concept of Loop in Swift

Looping :-

                Looping, also known iteration is used in a program to repeat a specific block/segment of code(s). This reduces the tasks of writing the same thing again and again. 


for condition increment loops:-

                The for-condition-increment loop is functionally the same as the 'for' loop in C. The loop consists of an initialization phase, a test, an increment, and a set of statements that are executed for every iteration of the loop.

for initialization; condition; increment {
statements
}

for in loops:- 
                You use the for-in loop to iterate over sequences or collections of things, such as the elements of an array or dictionary, the characters in a string, or a range of numbers. Here's the general form is:

for index in sequence {
statements
}

In the above example, this iterates over a range of numbers, the loop index variable (i) takes on the value of the next number in the range each time through the loop:








while loops:-

While loops test the condition ahead of the loop's body and only if the condition evaluates to true, then the loop body gets executed. The general format is as follows:

while condition {
statements
}

Programmers can use the while loop to replicate the functionality of the for-condition-increment loop, as follows:










Repeat while/Do while:-

Repeat-while loops test the termination condition at the end of the loop, rather than at the start. This means the statements in the body of the loop are guaranteed to be executed at least once. Loop execution continues until the condition evaluates to false. The general format for a repeat-while loop looks like this:

repeat {
statements
} while condition

Here's an example of repeat while loop:










There are 3 types of loops in Swift:-

for in loop
while loop
repeat...while loop

Swift for-in Loop:-
In Swift, the for-in loop is used to run a block of code for a certain number of times. It is used to iterate over any sequences such as an array, range, string, etc.

The syntax of the for-in loop is:

for val in sequence{
  // statements
}
Here, val accesses each item of sequence on each iteration. Loop continues until we reach the last item in the sequence.

FOR-IN LOOP WITH COLLECTIONS:-
This example iterates over an array of cities, also known as a collection in Swift.





// Amsterdam
// New York
// San Francisco

FOR-IN LOOP WITH DICTIONARIES:-
This example iterates over a dictionary. Here, it prints out the age of each person.




// Antoine is 28 years old
// Jaap is 2 years old
// Jack is 72 years old

ForIn vs forEach:-
AN EXAMPLE OF THE DIFFERENCES

The following example collects the first 10 even numbers of a collection of numbers. For this, we’re using the break and continue statements. The break statement stops the loop, while the continue statements are used to skip the current number.










// [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

While loops:-

While loops are less often used but can be really useful. It’s basically executing its statements while the condition is true. The following example is rolling dice until the max amount of tries is reached.










for Loop with Stride Function
If we want a loop to increment by some fixed value in each iteration, instead of range, we have to use stride(from:to:by) function. 

For example:-









In the above example, the loop iterates over the range from 1 to 10 with an interval of 2.

Comments

Post a Comment

If you have any douts, Please let me know

Popular posts from this blog

POWER BI

Face Recognition

BOOTSTRAP