Ok so lets say I have boolean x = false
. If I have a loop that says while (!x)
does this mean while x is NOT false (true) or while x is NOT true (false)?
EDIT: Ok I'm a bit confused, I think Im getting different answers. So if I have
int x=0;
boolean add1=false;
while (!add1){
x=1;
}
What is the final value of x in this case?
x=false;
while (!x)
substituting the value of x
we have
while(!false)
Now !
is the logical complement operator which inverts the value of a boolean so we get
while(true)
So we keep looping till x
is false
or till !x
is true
.
Note that the value of the variable can change in the body of the loop causing the looping to break. Consider a typical search program which uses a boolean variable called found:
found = false; // initialize found to false.
while(!found) { // keep looping till key is not found.
...
if(key is found) {
found = true; // key found...make found true.
}
}
UPDATE:
In your updated question add1
is false
initially so !add1
is true
so we enter the while loop and change x
to 1
. Since the value of add1
does not change in the loop and there are no break
and return
you keep looping infinitely.
The loop will repeat as long as !x
is true. In other words, it will repeat as long as x
is false.
A boolean condition is always checked for being true. So while (!x)
means while x is not true, ie while x is false.
Update: The code you posted is an infinite loop since !add1
evaluates to true and this is never changed within the loop.
I feel nobody really (tried to) explain, what it means .
while (!x)
does not really mean much. It is only a few instructions for the computer to do. It does not mean things like do stuff while x is true or false or whatever. !x
is an expression and in the case that x = false
, this expression yields true
. The computer actually calculates true
by applying !
on the value of x
: false
.
while(!x)
does not mean "run when x equals false." It means something like "run if inverting the truth value of variable x yields true." From experience we know (or learn) that it is about the same as "run only when x equals false". Just like while (!(x && !x))
means(?) "run always". You can see this by trying all possible values of x and remembering those who will make the expression equal true. Some day you will just know what it 'means'.
What it does , how it works , is not what it means , right?
while(!add1) runs if add1 is false. Since add1 is always false, x will always equal 1.
The loop will continue to iterate so long as x = false. If x = true the loop will end.
Let's replace x
with red sky
. While the sky is not red, red sky = false
will evaluate as true
. Asking repeatedly whether the value red sky
is not true (as long as the sky stays blue, of course) will cause the loop to repeat.
I think you have a code smell here.
I prefer to have simple while conditions, to keep the code readable. So if you end with a piece of logic that looks like:
boolean is_something = false;
while (!is_something) {
...
}
maybe it's better to reverse both the meaning of the variable and its default value, to get rid of the negation in the while condition:
boolean is_not_something = true;
while (is_not_something) {
...
}
Please note that this is a very general example. Often you don't need to have an explicit negation in the variable name. If you have a variable called is_empty
, the opposite doesn't have to be called is_not_empty
, it can be called is_full
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.