# Mientras que el ciclo con && termina si alguno es verdadero, en lugar de si ambos son verdaderos

I have this while loop that always ends if one of the cases is true, instead of only ending if both are true. Does anyone have any clue why?

``````while (f != NULL && t!= NULL)
{
if ( f == NULL && t!= NULL)
{
cout << t->start_byte << " - "<< left<< setw(20) << t-> end_byte <<  t->id << endl;
t=t->next;
}
else if (t == NULL && f != NULL)
{
cout << f->start_byte << " - "<< left<< setw(21) << f-> end_byte << "FREE" << endl;
f=f->next;
}
else
{
cout << f->start_byte << " - "<< left<< setw(21) << f-> end_byte << "FREE" << endl;
cout << t->start_byte << " - "<< left<< setw(21) << t-> end_byte <<  t->id << endl;
cout<< " IM IN THE BOTH " << endl;
t=t->next;
f=f->next;
}
}
``````

The output is as follows, keep in mind that in this particular example the loop should have one more FREE output at the bottom because my f linked list has 3 nodes, and my t linked list has 2

``````  Memory Block Job

0 - 99                   FREE
100 - 129                  3
IM IN THE BOTH
130 - 149                  FREE
150 - 179                  2
IM IN THE BOTH
``````

preguntado el 12 de febrero de 14 a las 08:02

"instead of only ending if both are true" Quiere decir if either condition is false? -

## 3 Respuestas

``````while (f != NULL && t!= NULL)
``````

will evaluate true only if both `f` y `t` are non-NULL.

Judging from the code inside the loop, you want it to run while either variable is non-NULL. You need to use logical OR `||` en lugar:

``````while (f != NULL || t!= NULL)
``````

Respondido 12 Feb 14, 08:02

Such a silly mistake on my part, thank you very much! I'm not sure why i was not thinking it through like that. I had it stuck in my mind that the opposite would happen and did not even think to try the other option. - Shane1022

Cuando tengas `boolean1 && boolean2`, here are possible scenarios:

``````boolean1 |    boolean2    | result
---------+----------------+-------
true   |      true      | true
true   |      false     | false
false  | doesn't matter | false
``````

Cuándo `boolean1` is `false`, Debido a evaluación de cortocircuito, `boolean2` won't be even reached, since the result is `false` anyway. So in this case the loop condition will be satisfied , solamente if the two sides are `true`.

Es posible que desee utilizar `||` en lugar de `&&`:

``````while(f != NULL || t!= NULL)
``````

And now the condition will be satisfied if only one of the two is `true`.

Respondido 12 Feb 14, 09:02

La `true`/`false` case is not covered by your table. - fredodesbordamiento

The loop is running as long as both conditions are true, and ends as soon as one of them is false.

If you want to it to run as long as one of them is true, and end as soon as both of them are false, then:

Cambie el `&&` a `||`.

Respondido 12 Feb 14, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.