Repeat loop in Swift

Pawan Manjani
Dec 24, 2023

--

As we know we having for loop and while loop in swift for writing loops but also we have repeat loop.

In repeat loop the loop block will be execute at least once even condition get fails.

repeat {
print("block executed")
} while number > 0

/* Output will be
block executed
*/

In above example even condition is false but still block will be execute once because condition will be execute at the end of the repeat loop code.

Gif Source: https://giphy.com

Let’s have one more example

var number = 1
repeat {
print(number)
number += 1
} while number < 5

/* Output will be
1
2
3
4
*/

Please like and follow me for more

Thanks for reading, Happy Coding 💻

--

--

No responses yet