# Unit 5 Assessment 3 Question 10

**URL:** <https://forum.code.org/t/unit-5-assessment-3-question-10/6843>\
**Category:** CS Principles\
**Created:** [February 28, 2017, 4:38pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843 "2017-02-28T16:38:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cconnor](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cconnor](https://forum.code.org/u/cconnor)\
**Post date:** [February 28, 2017, 4:38pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/1 "2017-02-28T16:38:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hwalden](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/hwalden/32/2254_2.png) [@hwalden](https://forum.code.org/u/hwalden)\
**Post date:** [March 1, 2017, 9:20pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/2 "2017-03-01T21:20:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cconnor](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cconnor](https://forum.code.org/u/cconnor)\
**Post date:** [March 1, 2017, 9:41pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/3 "2017-03-01T21:41:14Z")

</div>

Thanks - got it now!

---

<div class="post-metadata">

**Author:** ![baker](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/baker/32/1229_2.png) [@baker](https://forum.code.org/u/baker)\
**Post date:** [March 1, 2017, 10:35pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/4 "2017-03-01T22:35:28Z")

</div>

Good. Worth noting that this question is intentionally tricky on English and logic not always getting along 🙂 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.

NERD TALK:

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~

---

<div class="post-metadata">

**Author:** ![cconnor](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cconnor](https://forum.code.org/u/cconnor)\
**Post date:** [March 1, 2017, 11:46pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/5 "2017-03-01T23:46:02Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![lamberto\_roscioli](https://avatars.discourse-cdn.com/v4/letter/l/6bbea6/32.png) [@lamberto\_roscioli](https://forum.code.org/u/lamberto_roscioli)\
**Post date:** [March 6, 2017, 12:39pm UTC](https://forum.code.org/t/unit-5-assessment-3-question-10/6843/6 "2017-03-06T12:39:17Z")

</div>

They are doing it for a living! 😉
