Monday, February 13, 2017

Logical Operators

Logical operators can be used with if and if-else to simplify and extend decision making.
The three logical operators are OR (||), AND (&&) and NOT (!) which are explained and demonstrated in this part of the course.

The OR Logical Operator (||)

The OR logical operator is written in sketches as two vertical pipe symbols (||) found on the same key as the backslash (\) on USA and other keyboards. Pressing Shift + \ (Shift and back slash keys) will type the vertical pipe character.
The following sketch demonstrates the use of the OR logical operator to check for the upper and lower-case versions of an alphabet character.
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

void loop() {
  char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       //
    if (rx_byte == 'a' || rx_byte == 'A') {
      digitalWrite(13, HIGH);
    }
    else {
      digitalWrite(13, LOW);
    }
  }
}
The sketch will switch the LED on the Arduino Uno board on if the lower-case character 'a' or the upper-case character 'A' is sent from the serial monitor window. If any other character is sent, the LED is switched off.

How the Logical OR Operator Works

The code below is taken from the above sketch and shows the logical OR operator.
if (rx_byte == 'a' || rx_byte == 'A') {
  digitalWrite(13, HIGH);
}
The code in the body of the if statement will run if the variable rx_byte contains 'a' OR (||) if it contains 'A'. The OR operator has been used to test for one or the other character (A OR a).
The code can be modified to switch the LED on if the character 'a' or the character 'b' or the character 'c' is received, as this next code demonstrates.
if ((rx_byte == 'a') || (rx_byte == 'b') || (rx_byte == 'c')) {
  digitalWrite(13, HIGH);
}
In the above code, each equal to relational operator comparison has been put in parentheses () to make the code easier to read. This also avoids any misunderstanding about which operator is evaluated first (does the == or the || get evaluated first?).
The == has a higher precedence than the || which means that == is evaluated first. Parentheses have the highest precedence, so anything placed in parentheses will be evaluated first. In this case it is not necessary to place the parentheses, but makes it easier to read.

The AND Logical Operator (&&)

The next sketch demonstrates the use of the logical AND operator. The sketch tests to see that a sequence of two characters has been received before turning the LED on.
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

char first_char = 0;

void loop() {
  char rx_byte;
  
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       // read the character
    if ((first_char == 'c') && (rx_byte == 'd')) {
      digitalWrite(13, HIGH);
    }
    else {
      first_char = rx_byte;        // save the character for next comparison
      digitalWrite(13, LOW);
    }
  }
}
In this sketch, two characters must be sent in the right order to switch the LED on. First 'c' must be sent, followed by 'd'.
This video shows the above sketch operating:

How the Logical AND Operator Works

The AND operator from the above sketch is shown below.
if ((first_char == 'c') && (rx_byte == 'd')) {
  digitalWrite(13, HIGH);
}
The LED will only switch on when the variable first_char contains 'c' AND the variable rx_byte contains 'd'.
The variable first_char is used to store the current character received so that the next time the if statement is evaluated, we can see if it was followed by 'd' and if it contains 'c'.

The Logical NOT Operator (!)

The NOT operator can be used to check if a variable contains the value 0 – in other words it can be used to check if a variable evaluates to false.
int x = 0;
if(!x) {
 // if not x - if x evaluates to false, code here will run
}

// this code is another way of writing the above code
if (x == 0) {
}

No comments: