U4L9 Logic Operator Precedence

Running the stations on DeMorgan’s Laws, some students are getting different answers for A || B && C

One set of students are doing the OR first, the other set is doing the AND first.

Code.org answer key has the OR first, but online it looks like the order of operators is NOT, AND, then OR.

Is Code.org wrong on the key, or is this by design?

I’d say it is working as intended this has to deal with how operators work with Parenthesis to specify importance of the operation

operations will always start at the left-hand evaluation (meaning if the 1st && operator equates to false it won’t evaluate the rest since && needs both sides to be true, no point on testing if one side is already false)

to show this in action let’s go step by step in a given program

var a = false;
var b = false;
var c = true;
console.log(a && b || c);
// here a is false and b is false but c is by itself meaning
// 1 side of the || is true resulting in true
console.log(a || b && c);
// here a is false while b is also false tied to an &&
// which is true since it's evauluating b&&c along with the ||
// both sides are in fact false

by changing what values get tested by you also change the boolean result.
I’m not to familiar with the person behind these laws you can change what gets evaluated first by using () as well !(Not) simply flips a boolean evaluation
for example:

var a = false;
var b = true;
console.log(b&&(a || b))

something like this will force the || evaluation to happen first while being tied to the second half of the and statement and you can group as nexessary

Here is a picture of my truth table. Depending on which boolen expression you solve for first, it changes the outcome. There were no parentheses in the question or the exemplar. Just A || B && C.

The Code.org worksheet exemplar says the (A || B) && C is correct, but the Order of Operations I have found all over the internet say A || (B && C) should be the one that is correct.

Well if it’s a phrasing correction than I’d recommend taking it up with support since there the ones that usually handle course corrections i thought you were actually asking if CDO’s truth evaluations were faulty

1 Like

Hey @dherbert, thank you so much for posting your question to the forum! I agree with with you that A || B && C evaluates like A || (B &&C). I share this precedence table with my students and it shows && having precedence over ||. I also ran the truth table for A = true, B = true, and C = false and got the following:

So this also implies that the && is the first operation.

@varrience is correct, when problems like this come up in curriculum documents its always best to email support@code.org or use the “Rate this Lesson” button from the Unit landing page:

I went ahead and submitted “The answer key for station D is incorrect. It evaluates the || operation first in the expression A || B && C when it should evaluate the && operation first.”

If you want to copy and paste my review of the lesson I think that multiple reviews of the same lesson get higher up on the priority list an fixes come a bit faster.

Good catch on this! Thanks again for bringing this up. Discussions like this really iron out the curriculum and make it better.