For faster navigation, this Iframe is preloading the Wikiwand page for break文.

break文

この記事は検証可能参考文献や出典が全く示されていないか、不十分です。 出典を追加して記事の信頼性向上にご協力ください。(このテンプレートの使い方)出典検索?"Break文" – ニュース · 書籍 · スカラー · CiNii · J-STAGE · NDL · dlib.jp · ジャパンサーチ · TWL (2011年2月)

break文は、プログラミング言語ループ制御構造で用いられる文で、最も内側のfor文while文do-while文から脱出する。また、C言語のようなswitch文がある言語では、(ループではないが)switch文から脱出する際にも使用される[1]

構文と意味

[編集]

C言語、およびそれに仕様を類似させている言語が備えるのひとつであり、構文としてはキーワード break とセミコロンから成る。Perlではlastというキーワードである。

  break;

ループ中(またはswitch文)の内部で実行されると、ループ全体(またはswitch文)を打ち切って、次の文へと制御を移す。

[編集]

C言語において、単純なループからbreak文を使って抜ける例を示す。

while (1) {
  if (条件式) break;
}

この例では、if文の条件式が成立するとbreak文が実行され、このループの直後に制御が移される。

switch文から脱出する例も示す。詳細はswitch文を参照。

switch () {
case 定数式:
  
  break;
default:
  
  break;
}

多重ループからの抜け出し

[編集]

C言語

[編集]

最初の例として、内側のループからの抜け出しを示す。

while (1) {
  while (1) {
    if (条件式) break;
  }
}

このコードのbreak;は内側のループのみから抜け出す。外側のwhileループは無限ループになる。

次に、多重ループからの抜け出しについて論じる。

C言語では、多重ループから一気に抜ける際にはgoto文を使用することができる[2]

while (1) {
  while (1) {
    if (条件式) goto label;
  }
}
label: ;

もしgoto文を使わずにbreak文を使って書くとすれば、一例としては以下のようになり、複雑かつ冗長となる。複雑度の上昇はバグの混入を許す原因ともなる。

int flag = 0;
while (1) {
  while (1) {
    if (条件式) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
     break;
  }
}

ほかにも、関数をそのまま脱出して呼び出し元に制御を返してよいということが前提にあるのであれば、多重ループを脱出するのにreturn文を使うこともできる。

while (1) {
  while (1) {
    if (条件式) return;
  }
}

このような場合について、gotoを使うのと同程度に明瞭簡潔に書けるようにするため、次節以降で述べるように、gotoを廃した言語では、ラベル付きbreakなどの「弱められたgoto」と言えるような機能が考案されている。

Java および JavaScript

[編集]
label: // 外側のwhile文にラベルをつける
  while (true) {
    while (true) {
      if (条件式) break label;
    }
  }

JavaJavaScriptはgoto文をサポートしておらず、代わりにラベル指定付きのbreak文を使うことで多重ループから一気に抜けることができる。

PHP

[編集]
while (true) {
  while (true) {
    if (条件式) break 2;
  }
}

PHPではgoto文がバージョン5.3から実装されたが、break文にループを抜ける段数を指定することも可能である[3]

脚注

[編集]
  1. ^ このbreak文の性質上、ループ脱出のためのbreak文を含むif文をswitch文で置き換えようとする際には注意が必要である。一例としてはそのようなコードによるバグが、1990年1月15日に米国AT&Tの長距離電話網が大規模に麻痺する障害を起こした。詳細は「All Circuits are Busy Now: The 1990 AT&T Long Distance Network Collapse」を参照。en:List of software bugs#Telecommunicationsも参照。
  2. ^ 最後の「label:」の後のセミコロンは「空の文」である。C言語の標準仕様では、ラベルは「ラベル付き文」としてしか存在できないので、ラベルの後に何もしない場合には構文規則上「空の文」を追加しなければならない(多くの処理系では、仕様を拡張しているが[要出典])。
  3. ^ break PHPマニュアル、2015年4月3日閲覧。

関連項目

[編集]
{{bottomLinkPreText}} {{bottomLinkText}}
break文
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?