Created
February 8, 2017 15:55
-
-
Save Sankame/2f608da7dbbc9313462a1bb4cbc0c1d0 to your computer and use it in GitHub Desktop.
Swift3のループ処理
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
// ***************************************** | |
// 1 から 5 までループ | |
// ***************************************** | |
print("-----<A-1>-----") | |
for index in (1...5) { | |
print(index) | |
} | |
print("-----<A-2>-----") | |
(1...5).forEach { (index) in | |
print(index) | |
} | |
print("-----<上記A-2の簡略表記>-----") | |
(1...5).forEach{ print($0) } | |
// ***************************************** | |
// 5 から 1 までループ | |
// ***************************************** | |
print("-----<B-1>-----") | |
for index in (1...5).reversed() { | |
print(index) | |
} | |
print("-----<B-2>-----") | |
(1...5).reversed().forEach { (index) in | |
print(index) | |
} | |
print("-----<上記B-2の簡略表記>-----") | |
(1...5).reversed().forEach{ print($0) } | |
// ***************************************** | |
// 配列を走査 | |
// ***************************************** | |
print("-----<配列を走査>-----") | |
let arrayNormal = ["a", "b", "c", "d", "e"] | |
for item in arrayNormal{ | |
print(item) | |
} | |
// ***************************************** | |
// 配列を走査(ループカウンター利用) | |
// ***************************************** | |
print("-----<配列を走査(ループカウンター利用)>-----") | |
let arrayCounter = ["A", "B", "C", "D", "E"] | |
for (index, element) in arrayCounter.enumerated() { | |
//indexが偶数の要素のみ出力。 | |
if (index % 2) == 0 { | |
print(element) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment