Created
October 17, 2021 08:12
-
-
Save deathlezz/d1f2b0017c1cbb3aa753fbe4af2facf8 to your computer and use it in GitHub Desktop.
Simple use of loops in Swift 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Simple use of loops | |
// | |
// for loop | |
let range = 1...5 | |
for i in range { | |
print("Number is \(i)") | |
} | |
// Number is 1 | |
// Number is 2 ... | |
let names = ["Jerry", "Kevin", "Bob", "Harold"] | |
for name in names { | |
print("It is your time \(name)") | |
} | |
// It is your time Jerry | |
// It is your time Kevin ... | |
// while loop | |
var number = 2 | |
while number > 0 { | |
print(number) | |
number -= 1 | |
} | |
print("Here is Johnny!") | |
// 2 | |
// 1 | |
// Here is Johnny! | |
// repeat loop | |
var amount = 1 | |
repeat { | |
print(amount) | |
amount += 1 | |
} while amount < 10 | |
print("Here I come!") | |
// 8 | |
// 9 | |
// Here I come! | |
// infinite loop | |
var counter = 1 | |
while true { | |
print(counter) | |
counter += 1 | |
if counter == 4 { | |
break | |
} | |
} | |
// 1 | |
// 2 | |
// 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment