基本的なor文の書き方のメモ。
個人的に、until(1..10で10を含まない)の書き方がわからず調べたので。
for (i in 1..4) //カウントアップ
カウントアップするfor文の書き方。
for (i in 1..4){
print(i)
}
出力結果:1234
for (i in 4 downTo 1) //カウントダウン
カウントダウンするfor文の書き方。
for (i in 4 downTo 1)
print(i)
}
出力結果:4321
for (i in 1..8 step 2) //2つ飛ばしでカウントアップ
2つ飛ばしでカウントアップするfor文の書き方。
for (i in 1..8 step 2) {
print(i)
}
出力結果:1357
for (i in 8 downTo 1 step 2) //2つ飛ばしでカウントダウン
2つ飛ばしでカウントダウンするfor文の書き方。
for (i in 8 downTo 1 step 2) {
print(i)
}
出力結果:8642
for (i in 1 until 10) //end要素を含まないカウントアップ
end要素を含まないカウントアップfor文の書き方。
for (i in 1 until 10) {
print(i)
}
出力結果:123456789
※この場合、10未満。10を含まない。
参考サイト
https://kotlinlang.org/docs/reference/ranges.html
コメント