“If(条件判断语句) 和 比较运算符”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
(以“if,它与比较运算符一起使用,用于判断条件是否满足,如一个输入值是否大于指定值。if语句语法格式: <pre style="color:dimgray...”为内容创建页面)
 
第40行: 第40行:
 
 谨防误使用单等号(例如if(x = 10))。单等号是赋值运算符,是用来设置x的值为10(将值10存入变量x中)。使用双等号(例如if(x == 10)),这是比较运算符,用来测试x是否等于10。后者只有当x等于10时为真,而前者总是真。
 
 谨防误使用单等号(例如if(x = 10))。单等号是赋值运算符,是用来设置x的值为10(将值10存入变量x中)。使用双等号(例如if(x == 10)),这是比较运算符,用来测试x是否等于10。后者只有当x等于10时为真,而前者总是真。
  
 这是因为C语言中 该语句if(x=10)是这样执行的:10被赋值给了x(记住单等号是[[= 赋值运算符|赋值运算符]])
+
 这是因为C语言中 该语句if(x=10)是这样执行的:10被赋值给了x(记住单等号是[[= ( 赋值运算符)|赋值运算符]])
 
This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.
 
This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.
  

2015年6月4日 (四) 17:22的版本

if,它与比较运算符一起使用,用于判断条件是否满足,如一个输入值是否大于指定值。if语句语法格式:

if (someVariable > 50)
{
  // 在此添加执行代码
}

上面的程序为判断someVariable 是否大于50。如果大于,执行指定代码。换句话说,如果if后圆括号中的语句为真,则执行大括号中的代码。反之,则跳过大括号中的代码。

if语句后的大括号可以省略。如果这样写,下一行的语句(以分号结尾)将是唯一条件语句

if (x > 120) digitalWrite(LEDpin, HIGH); 

if (x > 120)
digitalWrite(LEDpin, HIGH); 

if (x > 120){ digitalWrite(LEDpin, HIGH); } 

if (x > 120){ 
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); 
}                                 // 以上几种书写方式都正确

在圆括号中的语句需要使用一个或多个运算符:

比较运算符:

 x == y (x is equal to y)
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x >  y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y)

警告:

谨防误使用单等号(例如if(x = 10))。单等号是赋值运算符,是用来设置x的值为10(将值10存入变量x中)。使用双等号(例如if(x == 10)),这是比较运算符,用来测试x是否等于10。后者只有当x等于10时为真,而前者总是真。

这是因为C语言中 该语句if(x=10)是这样执行的:10被赋值给了x(记住单等号是赋值运算符) This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

if can also be part of a branching control structure using the if...else construction.


返回Arduino语法参考列表

原文出处:Arduino官网

更多建议和问题欢迎反馈至 YFRobot论坛