An if statement performs a test of a given condition. If this condition is “true,” then the statement that follows the if is executed. However, if the condition is not true (in other words, it is false), then the statement following the if is not executed. The condition is enclosed in parentheses. If we want an if statement to control the execution of multiple statements, these statements must be enclosed in braces. (Even when controlling the execution of a single statement, it is permissible to include that single statement in braces. We will use braces whether or not we are trying to control the execution of multiple statements.) The following is a “template” for an if statement:
if (condition) {
// Statements in braces only executed if the condition is true.
}
The condition can, in fact, be any expression (anything that returns data). If the expression yields a result that is zero, the condition is considered false. Any other value is considered true. Often the condition employs a comparison operation. Comparison operations are considered in the following section.
We can also associate an else clause with an if statement. When present, the code associated with the else clause will not be executed if the condition is true, but will be executed if it is false. A template for an if statement that includes an else clause is as follows:
if (condition) {
// Statements only executed if the condition is true.
}
else {
// Statements only executed if the condition is false.
}
The condition that controls the action of the if statement can take the form of a comparison expression. A comparison is accomplished using a comparison operator, which is often called a relational operator. Examples of relational operators include < (less than), > (greater than), >= (greater than or equal to), == (equal to), and != (not equal to).
A common programming error is to write a single equal sign instead of two equal signs when trying to compare two values. Keep in mind that a single equal sign is the assignment operator (that assigns the value on the right of the equal sign to the thing on the left of the equal sign).
Assuming x is an integer variable, the following are examples of comparison expressions:
x == 0 // Is x equal to zero?
x != 0 // Is x not equal to zero?
x <= 5 // Is x less than or equal to five?
The first expression evaluates to true when the variable x equals zero, and false for any other value. The second expression evaluates to false if x equals zero, and true for any other value. The third expression evaluates to true if x is less than or equal to five. The following table lists all the relational operators:
Symbol |
Description | Example |
---|---|---|
< | Less Than |
7 < x True if 7 is less than x. Said another way, true if x is greater than 7. False if 7 is greater than or equal to x. |
<= | Less Than or Equal To |
x <= 0 True if x is less than or equal to zero. False if x is greater than zero. |
== | Equal To |
7 == x True if x equals 7. False if x is not equal to 7. |
!= | Not Equal To |
x != 123 True if x is not equal to 123. False if x is equal to 123. |
>= | Greater Than or Equal To |
7 >= x True if 7 is greater than or equal to x. False if 7 is strictly less than x. |
> | Greater Than |
x > 97 True if x is greater than 97. False is x is less than or equal to 97. |
Keep in mind that these types of comparisons can be used as the “condition” that controls the behavior of an if statement. With that in mind, an example of an if statement that could be used (with proper initialization) to have a button control an LED is:
// btnPinA assumed to correspond to a button pin.
// ledPinA assumed to correspond to a LED pin.
if (digitalRead(btnPinA) == HIGH) {
digitalWrite(ledPinA, HIGH);
}
else {
digitalWrite(ledPinA, LOW);
}
The first thing that happens when this if statement is executed is the evaluation of the condition in the parentheses in line 3. This condition has, on the left side of the comparison operator, the digitalRead() function. This function is executed, where the assumption is that the parameter btnPinA corresponds to the pin attached to a button labeled “button A.” The value this function returns is compared to the constant value HIGH. If the value read is HIGH, line 4 is executed. If the value is not HIGH (i.e., if it is LOW), then line 7 is executed. The assumption is that ledPinA corresponds to the pin to which an LED known as LED A is connected. Stated more plainly, the syntax reads, “if button A is pressed, turn on LED A, else turn off LED A.”