var x = 0;
var y = 0;
while (x < 10 && y < 20){
x += 1;
y += 1;
console.log('This is x: ', x)
console.log('This is y: ', y)
}
I don't think I understand how multiple conditions in loops work. What I expect that code to do is to continue to loop until x greater than 10 and y is greater than 20 however the loop stops once x is greater than 10. I thought the && means both conditions have to be met.
var x = 0;
var y = 0;
while (x < 10 && y < 20){
x += 1;
y += 1;
console.log('This is x: ', x)
console.log('This is y: ', y)
}
I don't think I understand how multiple conditions in loops work. What I expect that code to do is to continue to loop until x greater than 10 and y is greater than 20 however the loop stops once x is greater than 10. I thought the && means both conditions have to be met.
Share Improve this question asked Feb 8, 2016 at 16:18 Farris IsmatiFarris Ismati 2062 silver badges10 bronze badges 05 Answers
Reset to default 4I thought the && means both conditions have to be met
It does. x
is no longer less than 10, so the condition isn't met. You want an or, where either condition has to be met:
while (x < 10 || y < 20){
Though in this simple case, the loop is only controlled by the y
variable anyway as they're incremented at the same rate.
If you want BOTH conditions (x is less than 10 AND ALSO y is less than 20):
while (x < 10 && y < 20){
In your example x = 11, so this condition is not met.
However if you want EITHER of the conditions (either x is less than 10 OR y is less than 20):
while (x < 10 || y < 20){
You want x < 10 || y < 20
instead, or !(x >= 10 && y >= 20)
which you might find clearer judging by your question text.
But note that x
will run past 10.
I thought the && means both conditions have to be met
Actually it means both conditions MUST be met. As soon as one condition fails the loop ends.
As others pointed out if you want EITHER condition to be met you have to use an or:
while (x < 10 || y < 20){
As for your expected output, that cannot be met with just one loop. You need an extra condition:
while (y < 20){ // keep going until y == 20
if(x < 10) { // only increase/print x while < 10
x += 1;
console.log('This is x: ', x)
}
// always print y - since its the bigger one
y += 1;
console.log('This is y: ', y)
}
This only works because you know which number will bee bigger (y > x). If you dont know your limits in advance, but still want a similar oute you have to use a code like this:
int MAX_X = 10;
int MAX_Y = 20;
while (y < MAX_X || x < MAX_Y){ // keep going until both limits are reached
if(x < MAX_X) { // only increase/print x while < max_x
x += 1;
console.log('This is x: ', x)
}
if(y < MAX_Y) { // only increase/print y while < max_y
y += 1;
console.log('This is y: ', y)
}
}
this will keep going until the largest limit is reached (doesnt matter if its X or Y thats bigger), but stop printing out/increasing the smaller number
condition1 && condition2
will substitue condition1 & 2 with their respective boolean value (True/False).
In your case, as soon as x crosses 10, the expression bees
False && Condition2
which is always False
(Basic Boolean algebra).
Correct it with
x<10 || y<10