Unit 5 Assessment 3 Question 10

Code Studio Unit 5, Assessment 3 shows the answer to Q10 as “B” : rolls < 100 && sixes < 25. The problem states that the code should run until either rolls reaches 100, OR sixes reaches 25. I think the answer should be “C” - Can someone please explain? Thanks in advance.

The missing code fills in the condition that keeps the while loop running.

If we put into place the “C” option the while loop would look like this:

 while (rolls<100 || sixes < 25) {
    ... do this stuff..

this would cause a problem if sizes increased beyond 25 before rolls passed 100. Putting OR in the while loop means that it would continue to run if one OR the other condition is true.

If we put && (AND) in the condition, it will trigger as false if either option is false. So if sizes increases beyond 25 before rolls passes 100, it’s going to exit the loop because one condition is false, therefore the whole thing is false.

1 Like

Thanks - got it now!

Good. Worth noting that this question is intentionally tricky on English and logic not always getting along :slight_smile: And this is a classic gotcha.

“whichever comes first” is the tricky part (red herring) because that makes you think of a stopping case for the loop, and the stopping case is stated with an OR. But a while loop is the INVERSE of that - you’re stating the case under which things must continue.

So, you have to invert your brain and think: what are the conditions under which I need to keep rolling dice. Well I need to have NOT rolled 100 times AND NOT rolled 25 sixes. You need AND because you need both of those things to be true in order to roll the dice. Once either one becomes false (rolled 100 times or 25 sixes) then the loop stops.

FWIW I don’t think the actual AP test will have a “gotcha” like this, but it might if they decide to care about logic.


When I’m thinking about problems like this and I see something of the form A && B I like to say out loud to myself “it is the case that both A is true AND B is true”. For example: while (rolls < 100 && sizes < 25) becomes “As long as it is the case that both rolls < 100 and sixes < 25, then roll the dice”.

Similarly for OR I say “it is the case that EITHER A is true OR B is true”

If you weren’t in a multiple choice scenario, you could express the same logical truth using || (OR) if you wanted to. You’d just have to use NOT and invert the inequalities.

while ( ! ( rolls >= 100 || sixes >= 25) ) is logically equivalent to while (rolls < 100 && sixes < 25)

As long as rolls and sixes start at 0 and can only go up by 1 on each iteration you could even state it with OR and == like so (though it would be considered bad form):

while ( ! ( rolls == 100 || sixes == 25) ) which I would read as: “as long as it is NOT the case that either rolls is equal to 100 OR sixes is equal to 25” In English that sounds a little be closer to the original statement.

Logic is ~fun~


Thanks, Baker - you nailed it. I was focusing on the “whichever comes first” and they did indeed get me! Your explanation is really good - that combined with what Brooke shared does make logic “fun”! You guys are pretty good at this … maybe you should think about doing it for a living ~:wink:


They are doing it for a living! :wink: