Logical Operators

Logical Operator: And (&&)

Logical Operators

Logical Operator: And (&&)

Often we need to check if two or more conditions are simultaneously true. For example, in English we might say “if condition one and condition two are true, take some action.” Note that “and” requires that both the conditions are true—if either one is false, the action will not be taken. In C/C++ we use the logical operator “&&” to say “and”. For example, if we wanted to check if button A and button B were pushed, we might write something such as:

                  if (digitalRead(btnPinA) == HIGH && digitalRead(btnPinB) == HIGH) {
                     // Code only executed if buttons A and B are both pushed.
                  }
                  else {
                     // Code only executed if either or both buttons are not pushed.
                  }
                

Now assume we want to check if buttons A and B are pushed but button C is not pushed. The code for that might look like this:

                  if (digitalRead(btnPinA) == HIGH && digitalRead(btnPinB) == HIGH && digitalRead(btnPinC) == LOW) {
                     // Code only executed if buttons A and B are pushed and C is not pushed.
                  }
                  else {
                     // Code only executed if the above condition is not true.
                  }
                

The digitalRead() function returns either the constant HIGH or LOW, but these are actually integer values. Because this function returns an integer value, we can store the return value to an int variable. Thus, another way of writing the condition above is:

                  int btnStateA = digitalRead(btnPinA);
                  int btnStateB = digitalRead(btnPinB);
                  int btnStateC = digitalRead(btnPinC);
                  if (btnStateA == HIGH && btnStateB == HIGH && btnStateC == LOW) {
                     // Code only executed if buttons A and B are pushed and C is not pushed.
                  }
                  else {
                     // Code only executed if the above condition is not true.
                  }
                

If we need to check the state of a button only once within a sketch, there is really no compelling reason to store the state in a variable. However, if we need to check a button's state multiple times within a function, it's best to store the state in a variable. This will make the code slightly easier to read and is more efficient because the digitalRead() function does not have to be called multiple times (there is some “computational overhead” associated with calling a function).

Logical Operator: Or (||)

Instead of checking if multiple conditions are true, sometimes we merely need to check if one or another condition is true. For example, in English we might say “if condition one or condition two is true, take some action. ” Note that “or” merely requires that one or the condition is true, but both conditions could be true too. However, if both conditions are false, the action will not be taken. In C/C++ we use the logical operator “ ||” to say “or”. For example, if we wanted to check if button A or button B were pushed, we might write something such as:

                  if (digitalRead(btnPinA) == HIGH || digitalRead(btnPinB) == HIGH) {
                     // Code executed if either button A or B is pushed (or both are pushed) .
                  }
                  else {
                     // Code only executed if neither button is pushed.
                  }
                

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.