if 语句之后可以有一个可选的 else if ... else 语句,这对于使用single if ... else if语句测试各种条件非常有用。
当使用 if ... else if ... else 语句时,请记住 -
一个 if 可以有零或一个else语句,它必须在任何其他if之后。
if 可以有0到多个else if语句,它们必须在else之前。
一旦 else if 成功,将不会测试剩余的其他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语句执行顺序

- /* 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++;
- }
转载本站内容时,请务必注明来自W3xue,违者必究。