1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「..」以外のレンジ

Posted at
for (i in 10..1) println(i)

決まった回数回ループ処理をするときは「..」を使用するがそれ以外の書き方もあるので、メモする。

for (i in 1.rangeTo(10)) println(i)

for (i in 10 downTo 1) println(i)

rangeToは「..」と同じ意味を持つ。downToはrangeToと逆で大きい順にループさせる。

for (i in 0..10 step 3) println(i)
// 0 3 6 9

for(i in 0 until 3) println(i)
// 0 1 2

step nはnの回数だけ飛ばすことができる。untilは最後の値を含めない場合に使用する。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?