Loop In Swift - Learn Swift From Scratch - 03

Опубликовано: 23 Май 2019
на канале: Atif Naseem
56
4

here may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

#SwiftTutorial #SwiftForBeginners #AtifNaseem
Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. of a loop statement in most of the programming languages −

loops
Swift programming language provides the following kinds of loop to handle looping requirements. Click the following links to check their detail.


The for-in loop iterates over collections of items, such as ranges of numbers, items in an array, or characters in a string −

Syntax
The syntax of a for-in loop in Swift 4 programming language is −

for index in var {
statement(s)
}



A while loop statement in Swift 4 programming language repeatedly executes a target statement as long as a given condition is true.

Syntax
The syntax of a while loop in Swift 4 programming language is −

while condition {
statement(s)
}






Unlike for and while loops, which test the loop condition at the top of the loop, the repeat...while loop checks its condition at the bottom of the loop.

A repeat...while loop is similar to a while loop, except that a repeat...while loop is guaranteed to execute at least once.

Syntax
The syntax of a repeat...while loop in Swift 4 is −

repeat {
statement(s)
}
while( condition )