Hi,
can someone pls explain why the Boolean isExit variable is initially initialized to false and why the boolean in the while loop condition is set as true (!isExit) only for the Boolean in the if condition to be set to true.
It's quite confusing to me.
I found it hard to come up with the correct solution and eventually just downloaded the correct solution.
Java coffee
Level 4
Summation exercise with Boolean values
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Java coffee QA Automation Engineer
21 April, 01:49
Thanks so much for this explanation...it's somewhat clearer now.
+1
Thomas
20 April, 13:55
I do not know the code you're talking about but I have an idea what you may mean.
A loop is running as long as it's condition is true. E.g. this loop will run forever
Now you could replace the boolean true with a variable to be able to change the condition at runtime, like:
That's doing the same as the first code snippet. We just are using a variable. That variable we can use to end the loop at a certain point.
Now you might get the idea that the code doesn't read very nicely and that instead of 'loop while isRunning' it would be more pretty to have loop while not isExit.
For that you initialize isExit with false. But you know that the loop runs as long as the condition is true, means you need to negate the isExit false to true. The easiest possibility is to write an expression like isExit == false
The second possibility is to use the NOT operator. If you have a boolean false then this operator will negate that to true and the loop will continue running as long as the boolean is false.
+1