Created
October 2, 2022 14:51
-
-
Save AhmedMenaim/4e8bd0071d18ef7064975ff06748687f to your computer and use it in GitHub Desktop.
Menaim Academy - Swift Course - Control Flow - For loop, Repeat while, While loop
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
import Foundation | |
// MARK: - What are the Loops | |
// arr = [1,2,3,4,5] -> print(arr[0]), print(arr[1] | |
// MARK: - For-In Loops | |
let names = ["Ahmed", "Mohamed", "Menaim", "Mahmoud"] | |
for name in names { // name is a container | |
print(name) | |
} | |
//print(names[0], names[1]) | |
//print(names[1]) | |
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4] | |
for (animalName, legCount) in numberOfLegs { | |
print("\(animalName)s have \(legCount) legs") | |
} | |
for key in numberOfLegs.keys { | |
print(key) | |
} | |
for value in numberOfLegs.values { | |
print(value) | |
} | |
// MARK: - Ranges | |
for number in 1...5 { // => 1 => 5 | |
print(number) | |
} | |
for number in 1..<5 { // => 1 => < 5 | |
print(number) | |
} | |
// MARK: - While Loops | |
var number = 5 | |
//while number > 0 { // Infinite loop | |
// print(number) | |
//} | |
//while number > 0 { | |
// print(number) | |
// number -= 1 | |
//} | |
// MARK: - Repeat-While -> do-while | |
repeat { | |
print(number) | |
number -= 1 | |
} while number > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment