Help us understand the problem. What is going on with this article?

# 早見表

### 繰り返し処理

メソッド / 文法 繰り返し回数
for ~ in ~文 オブジェクトの要素分
while文 指定した条件がtrueの間
until文 指定した条件がfalseの間
timesメソッド 任意に指定
loopメソッド 無限

next スキップ
redo やり直し
break 中断

# 繰り返し回数を変化させるメソッド / 文法たち

## for ~ in ~

```for num in 1..5 do
p num
end
```

```1
2
3
4
5
=> 1..5
```

## while

```num = 0

while num <= 12 do
p num
num += 3
end
```

```0
3
6
9
12
=> nil
```

## until

```num = 16

until num <= 12 do
p num
num -= 2
end
```

```16
14
=> nil
```

## times

### 単純ループ

```num = 0

5.times do
p num
end
```

```0
0
0
0
0
=> 5
```

### 値を変化させながらループ

```num = 0

5.times do |num|
p num
num += 1
end
```

```0
1
2
3
4
=> 5
```

## loop

breakを使って止めない限り、無限に繰り返す

```num = 0

loop do
p num
num += 1
if num >= 3
break
end
end
```

```0
1
2
=> nil
```

# ループ処理中のテクニック

## next

スキップさせることができる

```num = 0..5

6.times do |num|
next if num == 1
p num
end
```

```0
2
3
4
5
=> 6
```

## redo

やり直しすることができる

```num = 0..5

6.times do |num|
num += 1
p num
redo if num == 1
end
```

```1
2
2
3
4
5
6
=> 6
```

## break

```num = 0..5

6.times do |num|
break if num == 3
p num
end
```

```0
1
2
=> nil
```
エンジニアが日本で一番成長できる環境を作り続ける
https://www.prum.jp/
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away