goto

名称

goto

説明

プログラム内のラベルに制御を移す。

書式

label:

goto label; // sends program flow to the label

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
for (byte r = 0; r < 255; r++) {
  for (byte g = 255; g > 0; g--) {
    for (byte b = 0; b < 255; b++) {
      if (analogRead(0) > 250) {
        goto bailout;
      }
      // more statements ...
    }
  }
}

bailout:
// more statements ..

注意

C言語ではgoto文の使用は推奨されていない。C言語関連の書籍の著者の中には、goto文は不要であると主張する人もいる。しかし、適切に使えば、ある種のプログラムを簡潔に記述することができる。多くのプログラマがgoto文の仕様に難色を示す理由は、goto文の乱用により、予期しないプログラムのフローを容易に作り出し、デバグができなくなることにある。

とはいうものの、goto文が重宝する状況はあり、コーディングを簡易にする。一つの状況は、深いループやifブロックから特定の条件の場合に抜け出すことである。

参照

オリジナルのページ

https://www.arduino.cc/reference/en/language/structure/control-structure/goto/

Last Revision: 2019/02/19

最終更新日

January 4, 2024

inserted by FC2 system