Farris Ismati February 2016

While loop multiple conditions don't check both conditions

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.

Answers


James Thorpe February 2016

I 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.


Bathsheba February 2016

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.


Gavriel February 2016

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){


Jithin Pavithran February 2016

condition1 && condition2 will substitue condition1 & 2 with their respective boolean value (True/False).
In your case, as soon as x crosses 10, the expression becomes
False && Condition2 which is always False(Basic Boolean algebra).

Correct it with
x<10 || y<10


Niko February 2016

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 become bigger (y > x). If you dont know your limits in advance, but still want a similar outcome 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

Post Status

Asked in February 2016
Viewed 1,970 times
Voted 7
Answered 5 times

Search




Leave an answer