I was trying to detect a cycle in a linked list. When I put the condition while(pointer!=NULL && pointer->next!=NULL)
, it worked all fine, gave all the desirable outputs. But when I inverted the condition, it stopped showing output when no cycle exists. Like when a cycle exists its giving true as output, but when it doesn't exist its giving no outputs. So, I'm wondering what could the reason be behind it.

- 555,201
- 31
- 458
- 770

- 1
- 2
-
3If the first condition is false, the second one is not checked. Checking `pointer->next` causes undefined behavior (most probably a crash) if `pointer` is null. – HolyBlackCat Jul 24 '21 at 18:37
-
4Look up "short circuit evaluation", for instance [How does C++ handle &&? (Short-circuit evaluation)](https://stackoverflow.com/questions/5211961/) – Remy Lebeau Jul 24 '21 at 18:37
2 Answers
Because the conditional is evaluated left-to-right. In your statement, 'pointer' and 'pointer->next' are values that you check against 'NULL'.
However, notice that accessing 'next' from 'pointer' requires that pointer itself be non-null. Trying to access an attribute from a null value results in undefined behaviour, which is what happened in your program
Thus, 'pointer' has to come before 'pointer->next' in the conditional statement. And, due to the use of the '&&' operator, the conditional is exited immediately if 'pointer' is null-checked first.

- 59
- 1
- 4
The benefit of && operator is that, wherever the condition gives first false value, the condition is not checked any further.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
struct ll{
int value;
struct ll* next;
};
struct ll* pointer;
while(pointer != NULL && pointer->next != NULL){
cout<< "pointer value:" << pointer <<endl;
}
return 0;
}
Note:In the above program, I have not allocated space to the pointer variable.
while(pointer!=NULL && pointer->next!=NULL)
Since the first condition returned false so further part of condition is not checked.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
struct ll{
int value;
struct ll* next;
};
struct ll* pointer;
while(pointer->next != NULL && pointer != NULL){
cout<< pointer->next << endl;
}
return 0;
}
Note:In the above program, I have not allocated space to the pointer variable.
while(pointer->next != NULL && pointer != NULL)
Here first condition is Invalid for comparision. So the program dumps here causing mostly segmentation fault.

- 19
- 4
-
3`pointer != NULL` is not necessarily false. Since `pointer` is uninitialized, reading it is UB. – HolyBlackCat Jul 24 '21 at 19:50
-
while printing the value of pointer just after its declaration, it shows value:0. The value of NULL is implementation defined, but is usually defined as the integer constant 0. So I thought its false as 0 != 0 is false.Thanks for pointing it out. – Gamer_at_Coding Jul 25 '21 at 03:47
-
Some compilers [will warn you about it being uninitialized](https://gcc.godbolt.org/z/3YTKKvn44). *"Thanks for pointing it out."* Consider editing your answer to fix it. – HolyBlackCat Jul 25 '21 at 06:01