0
int lim1, lim2, nr1, nr2;
lim1=1; lim2=1;
nr1=6; nr2=7;
while (nr1 != lim1 && nr2 != lim2) 
{
    nr1--; nr2--;
    cout << nr1 << ' ' << nr2 << endl;
}

Why does the while stop when nr1 gets to be 1, but nr2 doesn't. Isn't && supposed to mean that both conditions have to be met? nr2 isn't 1 at the end of the loop so why doesn't it become an infinite loop?

And why is this loop working? It is an infinite loop, as expected.

while (!(nr1 == lim1 && nr2 == lim2)) 
{
    nr1--; nr2--;
    cout << nr1 << ' ' << nr2 << endl;
}

Aren't both conditions checking the same thing?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56

4 Answers4

3

When nr1 is 1, then nr2 is 2. So when nr1 != lim1 is false then nr2 != lim2 is still true, but since the whole expression is false (because the logical AND) the loop ends. That is how logical AND works, both expressions have to be true for the whole expression to be true.

And the two conditions you have in the different loops aren't equal. De Morgan's laws says that

nr1 != lim1 && nr2 != lim2

is equal to

!(nr1 == lim1 || nr2 == lim2)

The laws also says that

!(nr1 == lim1 && nr2 == lim2)

is equal to

nr1 != lim1 || nr2 != lim2

You should also note that there is no "infinity" with integers. All integers are limited. The type int is on most systems a 32-bit signed integer, which means its range is about two billion to minus two billion. And when a signed integer over- or under-flows you will have undefined behavior.

So the end result of your infinite loop is undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

There is a simple mathematical formula, which is called De Morgan's formula, which says

In C++, is denoted by !. is denoted by $$, and is denoted by ||. So your expression must be converted to the following form !(nr1==lim1 || nr2==lim2) to be equvalent with (nr1!=lim1 && nr2!=lim2)

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
H.H
  • 305
  • 2
  • 9
0

Actually both condition do what they are supposed to do, so let's break them down.

First

while(nr1!=lim1 && nr2!=lim2) - Can be translated in english as, keep looping as long as both nr1 is different from lim1 and nr2 is different from lim2.

Second

while(!(nr1==lim1&&nr2==lim2)) - Can be translated to english as, keep looping as long as nr1 and nr2 are not equal respectively to lim1 and lim2.

The code doesn't do exactly what you want because nr1 and nr2 are set to 6 and 7. This means that in the first while loop nr1 will arrive first 1 and so will break the loop. Instead for the second condition it will always evaluate to true because there is not way for nr1 and nr2 to be contemporary equal to lim1 and lim2 respectively because there is a difference of 1 between nr1 and nr2.

Community
  • 1
  • 1
blazgrom
  • 196
  • 3
  • 7
0
nr1 != lim1 && nr2 != lim2

This statement is true if both:

  • nr1 is different from lim1
  • nr2 is different from lim2

So your program will stop iterating if at least one condition isn’t met.


nr1 == lim1 && nr2 == lim2

This statement is true only if both condtions are met. So if at least one condition isn’t met, the statement becomes false.

!(nr1 == lim1 && nr2 == lim2)

So now if you reverse the statement, it means it will be true if the statement was false. So with this condition, you’re telling your program: ”continue to iterate as long as at least one of the conditions is not met”. In other words, your program will iterate until both conditions are met.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56