Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

You need to write a while loop that continues until an integer x is in the set {2,3,4,5}. How would the loop be controlled? A) while (x > 2 && x < 5) B) while (x > 2 || x < 5) C) while (x < 2 && x > 5) D) while (x < 2 || x > 5)

OpenStudy (anonymous):

why it's not A) and why it is B) ?

OpenStudy (espex):

It is not A because the loop would break as soon as it crossed the threshold of 2 or 5, depending on which way it was going, this would cause the set to never include 3 or 4. Only by adding the OR as with B are you able to include all values between your bounds.

OpenStudy (anonymous):

It's not B, it's actually D. if you want a loop to continue, the boolean expression has to evaluate to true. so if x = 1 which of them would be true? A. 1 > 2 && 1 < 5 | false B. 1 > 2 || 1 < 5 | true C. 1 < 2 && 1 > 5 | false D. 1 < 2 || 1 > 5 | true Now it's either B or D if x = 2 B would be true (this is not what you want) D would be false (this seems to be what you want) you can think about some more values of x but D is correct

OpenStudy (anonymous):

yea....it's D

OpenStudy (anonymous):

can you give me example why it is not C?

OpenStudy (anonymous):

like above, if x = 1 C. 1 < 2 && 1 > 5 | false it would need to be true for it to work

OpenStudy (anonymous):

ok catch it....thnks

OpenStudy (espex):

My bad, I read it as you wanted to continue "while" x was within the set and not "until".

OpenStudy (anonymous):

me too

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!