这里会显示出您选择的修订版和当前版本之间的差别。
前一修订版 | |||
— | en:reference:language:if [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | =====if (conditional) and ==, !=, <, > (comparison operators)===== | ||
+ | |||
+ | if, which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is: | ||
+ | |||
+ | <code cpp> | ||
+ | if (someVariable > 50) | ||
+ | { | ||
+ | // do something here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code. | ||
+ | The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement. | ||
+ | |||
+ | <code cpp> | ||
+ | if (x > 120) digitalWrite(LEDpin, | ||
+ | |||
+ | if (x > 120) | ||
+ | digitalWrite(LEDpin, | ||
+ | |||
+ | if (x > 120){ digitalWrite(LEDpin, | ||
+ | |||
+ | if (x > 120){ | ||
+ | digitalWrite(LEDpin1, | ||
+ | digitalWrite(LEDpin2, | ||
+ | } // all are correct | ||
+ | </ | ||
+ | |||
+ | The statements being evaluated inside the parentheses require the use of one or more operators: | ||
+ | |||
+ | === Comparison Operators: === | ||
+ | |||
+ | < | ||
+ | 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) | ||
+ | </ | ||
+ | |||
+ | === Warning: === | ||
+ | |||
+ | Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true. | ||
+ | |||
+ | 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 can also be part of a branching control structure using the [[reference: | ||
+ | |||