If

if (condition) and ==, ! =, <, > (comparison operators)

if used in conjunction with comparison operators, checks if the condition is true, for example, if the input value exceeds a specified number. The format of the if statement is as follows:

if (someVariable > 50)
{
// perform actions
}

The program checks if someVariable value is greater than 50 or not. If so, certain actions are performed. In other words, if the expression in parentheses is true, the statements inside the curly brackets are executed. If not, the program skips this code.

The brackets after the if statement can be omitted. If so, only the next line (indicated by a semicolon) becomes the statement executed in the if statement.

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);
}
// everything OK

Expressions that are evaluated inside parentheses may consist 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 less than y)

x> y (x is greater than y)

x <= y (x less than or equal to y)

x> = y (x is greater than or equal to y)

Attention!

Take care not to accidentally use the simple equality sign (for example, if (x = 10)). The simple equality sign is an assignment operator, and sets the value of x to 10 (puts the value 10 in the variable x). Instead, use the double-equals sign (for example, if (x == 10)), which is a comparison operator and checks if x is 10 or not. The last of the two expressions will be true only if x is 10, but the previous expression is always true.

This is because C evaluates the expression if (x = 10) as follows: the value 10 is assigned to x (remember that the simple equality sign is an assignment operator), so x is now 10. Then the conditional if calculates 10, which already equal to TRUE, since any number, unequal 0, is TRUE. Therefore, if (x = 10) will always have the logical value TRUE, which is not the desired result when the if statement is used. In addition, the variable x will be assigned the value 10, which is also not the desired action.

If can also be part of an extensive control construct using if … else.