4.3: Boolean AND, NOT
Introduction
In 4.2: Pseudo-Code, Boolean OR we learned how to pseudo-code and use logical OR operators. In this module we will learn about the following.
Code control: how to control program inputs to quickly verify program logic.
Boolean values: what are boolean values and how are they stored in JavaScript?
Boolean AND operator
Boolean NOT operator
Conditional debugging: how to efficiently debug conditional logic.
Code Control
Sometimes we will want to control program inputs to verify program logic for specific inputs. This is especially relevant when we would otherwise not be able to control these inputs, for example when using a random number generator. Even though the user did not input the randomly-generated number, the number can be considered an input because it determines logic within the program.
No matter how small or large the range of randomness in our input, we want to be able to control this randomness during development to test our logic for potential inputs. In other words, we don't want to continuously enter numbers and click submit in our browsers until we reach a win state. If our dice rolls involved 100-sided dice, our hands would get tired.
We will control random number input to test winning conditions by removing randomness. We will "fix" the random number by changing rollDice
to always return a specific value. In the following code snippet, rollDice
always returns 6. Note that the return
keyword also ends the execution of the function: rollDice
never runs beyond line 2. We will remove return 6;
once we're done testing.
Boolean Values
Boolean operators are operators that return boolean values. Boolean operators consist of both logical operators (e.g. ||
, &&
, !
) and comparison operators (e.g. ==
, <
, >
). Boolean values are either true
, or false
. Boolean operators, similar to math operators (e.g. +
, -
, *
, /
), always return a new value that can be stored in a variable.
What is the value of myVal
in each of the following examples?
Math Operator Example
Boolean Operator Example
In previous modules we've seen number and string data types. Boolean data types are a 3rd data type. They represent a value that is true
or false
. Just like how variables can store the result of a math operation, variables can also hold the result of a logical, boolean operation.
Boolean Variable Example
Note the naming convention for variables that store booleans is to begin with a question word to signal that the variable's value is either true
or false
.
Boolean Operation and Assignment Example
Assign the result of input == randomDiceNumber
to didPlayerWin
. The result will be true
or false
. Notice this matches our dice game logic, which says that the player wins if their input matches the random dice number.
Dice Game Example
We will add another dice to our dice game, and update our logic such that the player only wins if both dice rolls are the same value as the player's guess. We will utilize the AND and NOT boolean operators for this task.
AND Operator
Use the boolean AND operator (&&
) to validate if 2 boolean statements are both true.
Pseudo Code
JavaScript
Dice Game with 2 Dice and Snake Eyes
Let's add another rule to our 2-dice dice game: the player loses if the 2 dice roll "snake eyes": 2 1s.
NOT Operator
Non-equivalence Example
Paired with =
, !=
is a "not equals" comparison operator, contrasting ==
. The following example runs line 2 if the value of diceRoll
is not 2.
Reverse-Boolean Example
Use the boolean NOT operator (!
) to "reverse" a boolean statement. The following code examples show a conditional that checks if the player did not win.
When comparing against true
or false
values, we might tend to write didPlayerWin != true
in our conditional.
However, the best practice is to put the !
operator in front of the boolean variable, like !didPlayerWin
. The following example is semantically the same as the previous example.
Encode Snake Eyes with NOT Operator
Dice Game with 2 Dice and Snake Eyes Pseudo Code
Snake Eyes Boolean Statement
The following statement encodes snake eyes in our program, where both diceRoll1
and diceRoll2
have a value of 1.
NOT Snake Eyes Boolean Statement
To express NOT snake eyes, we can negate the above boolean statement with the !
operator, making sure to surround the snake eyes statement with parentheses (()
) to indicate that the !
applies to the entire snake eyes statement and not just the first part. The following evaluates to true
when the dice rolls do not represent snake eyes, i.e. when diceRoll1
and diceRoll2
do not both have the value of 1.
Conditional Debugging
We've written pseudo-code, we've written JavaScript, we've controlled and tested our logic, yet our code could still have errors. Let's analyse common conditional errors and learn strategies to diagnose them.
Consider Common Conditional Errors
Boolean Expression Not Evaluating to True
When we use &&
, both boolean statements must be true for the combined statement to be true. If we only need 1 of the boolean statements to be true, use ||
instead.
Boolean Expression has Syntax Error
Did we unintentionally use the assignment operator =
instead of the comparison operator ==
?
Incorrect Logic in Boolean Statement
Are the parentheses in our boolean statement arranged such that the boolean logic matches what we wish to accomplish?
Deconstruct and Verify Each Condition
If the above common conditional errors are insufficient to debug our problem, we can use a more fine-grained debugging strategy. By now, many of our conditionals comprise multiple boolean statements connected by logical operators such as ||
, &&
, and !
. If the program behaviour is incorrect, we can methodically deconstruct our conditionals to verify each component.
For a given conditional like the following, we can console log each component of the conditional. We can then inspect each variable and boolean expression value. Note the helpfulness of labelling each console log for output clarity. Try not to introduce bugs while debugging by mislabeling console logs.
Sample Conditional
Sample Debugging Code
Exercises
Share your work with your section: save the code you write into another file. Name it share.js
(A file only for sharing in the community channel.) Send the code file as a snippet in your section community channel.
Follow Along
Duplicate and run the dice game versions above.
New Winning Conditions
Create new versions of our dice game for each of the following winning conditions.
User wins if guess is within 1 for any of 2 dice.
User wins if guess is within 1 for all 2 dice.
User wins if guess is within 1 of either dice but the user does not roll snake eyes.
User wins if guess is within 1 of either dice or if the user rolls snake eyes.
If the above exercises are taking a long time, we may wish to complete the subsequent pre-class modules before returning to these exercises.
Further Reading
Past students have found this slide deck helpful in understanding Boolean logic.
Last updated