2
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?

More than 3 years have passed since last update.

Swiftのループ文の早期退出はbreak/continue/return何を使えば良いか

Posted at

Swift書いてて、ループ文をreturnで抜けたとき、どこまで抜けるのかがよくわからなくなったので、サンプルコードを書いて確かめてみることにしました。

サンプル

Dice
struct Dice {
    func roll(from startIndex: Int, to endIndex: Int) {
        for i in (startIndex...endIndex) {
            guard i <= 6 else { return } //<-ここをreturn/break/continueそれぞれ試す
            print(i)
        }
        print("サイコロの目は6までです")
    }
}

もしreturn = breakなら、"サイコロの目は6までです"まで出力されるはずですね。
まずはbreakから。

break

Dice().roll(form: 1, to: 7)

これで挙動を試しました。

出力結果
1
2
3
4
5
6
サイコロの目は6までです

想定どおりですね。

continue

このサンプルだとcotinueとbreakの差がわかりづらいので、guard i == 6 else { continue }にして試します。

出力結果
6
サイコロの目は6までです

はい。
早期退出してもループ文は次の要素に対して継続するので、6のときだけprint出力が実行されます。

return

んで、returnなんですが……

出力結果
1
2
3
4
5
6

となりました。
よく考えたら当然で、returnで抜けるスコープはメソッドであって、ループ文から抜けるわけじゃないんですね。
(……と説明されると当たり前じゃん、と思ってしまうんですけど、実際にコード書いてると、いつの間にかbreak≒returnみたいな感覚になってました)

ちなみに

実際に使ったことはないんですけど、Swiftのbreak/continueにはラベルというのがつけられるそうで、

struct Dice {
    func roll(form startIndex: Int, to endIndex: Int) {
        parent: for _ in (startIndex...endIndex) {
            for i in (startIndex...endIndex) {
                guard i <= 6 else { break parent }
                print(i)
            }
            print("サイコロの目は6までです")
        }
    }
}

という風に、親スコープのループを抜けることができます。
この例だと、普通のbreakだと、上記のbreakを使ったときの出力例が7回出るんですけど、
ラベルをつけてあげると、1〜6を出力した時点で、親ループを抜けます。

使いどころはありそうなんですけど、実際に使ったことはないですね。

2
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
2
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?