switch…case

[Control Structures]

説明

if 文と同様に、switch case は、プログラマがさまざまな条件で実行する必要があるさまざまなコードを指定できるようにすることで、プログラムの流れを制御します。具体的には、switch 文は変数の値を case 文で指定された値と比較します。変数の値が一致する case 文が見つかると、その case 文のコードが実行されます。

The break keyword exits the switch statement and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions (“falling-through”) until a break, or the end of the switch statement is reached.

構文

switch (var) {
  case label1:
    // statements
    break;
  case label2:
    // statements
    break;
  default:
    // statements
    break;
}

媒介変数

var: 様々なケースと比較する値を持つ変数。使用可能なデータ型: intchar.
label1label2: 定数。許可されるデータ型: intchar.

戻り値

Nothing

switch (var) {
  case 1:
    //do something when var equals 1
    break;
  case 2:
    //do something when var equals 2
    break;
  default:
    // if nothing else matches, do the default
    // default is optional
    break;
}

Language Reference Home

86Duinoリファレンスのテキストは、Arduinoリファレンスを改変したもので、Creative Commons Attribution-ShareAlike 3.0ライセンスに基づいてライセンスされています。リファレンス内のコードサンプルはパブリックドメインとして公開されています。

上部へスクロール