Arduino-if-else-if-else-statement

提供:Dev Guides
移動先:案内検索

Arduino-If…else if…elseステートメント

*if* ステートメントの後にオプションの *else if ... else* ステートメントを続けることができます。これは、単一のif ... else ifステートメントを使用してさまざまな条件をテストするのに非常に便利です。
*if ... else if…else* ステートメントを使用する場合は、注意してください-
  • if にはゼロまたは1つのelseステートメントを含めることができ、else ifの後に来る必要があります。
  • if には、0個以上のelse ifステートメントがあり、elseの前になければなりません。
  • else if が成功すると、残りのelse ifまたはelseステートメントはテストされません。

if…else if…elseステートメント構文

if (expression_1) {
   Block of statements;
}

else if(expression_2) {
   Block of statements;
}
.
.
.

else {
   Block of statements;
}

if…else if…elseステートメント実行シーケンス

If Else If Elseステートメント

/*Global variable definition*/
int A = 5 ;
int B = 9 ;
int c = 15;

Void setup () {

}

Void loop () {
  /*check the boolean condition*/
   if (A > B)/* if condition is true then execute the following statement*/{
      A++;
   }
  /*check the boolean condition*/
   else if ((A == B )||( B < c) )/* if condition is true then
      execute the following statement*/{
      C = B* A;
   }else
      c++;
}