The order of operands of logical operators &&, || are important in C/C++.

In mathematics, logical AND, OR, etc… operations are commutative. The result will not change even if we swap RHS and LHS of the operator.

In C/C++ (may be in other languages as well) even though these operators are commutative, their order is critical. For example see the following code,

`// Traverse every alternative node ` `while` `( pTemp && pTemp->Next ) `
`{ ` ` ` `// Jump over to next node `
` ` `pTemp = pTemp->Next->Next; `
`} ` |

*chevron_right*

*filter_none*

The first part *pTemp* will be evaluated against NULL and followed by *pTemp->Next*. If *pTemp->Next *is placed first, the pointer *pTemp* will be dereferenced and there will be runtime error when *pTemp* is NULL.

It is mandatory to follow the order. Infact, it helps in generating efficient code. When the pointer *pTemp* is NULL, the second part will not be evaluated since the outcome of AND (&&) expression is guaranteed to be 0.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Evaluation order of operands
- Operators in C | Set 2 (Relational and Logical Operators)
- Operands for sizeof operator
- Code Optimization Technique (logical AND and logical OR)
- Written version of Logical operators in C++
- What are the differences between bitwise and logical AND operators in C/C++?
- Operators in C | Set 1 (Arithmetic Operators)
- Logical Not ! operator in C with Examples
- What are the operators that can be and cannot be overloaded in C++?
- Increment (Decrement) operators require L-value Expression
- Advanced C++ | Conversion Operators
- const_cast in C++ | Type Casting operators
- Bitwise Operators in C/C++
- How to sum two integers without using arithmetic operators in C/C++?
- Execution of printf with ++ operators
- Conditionally assign a value without using conditional and arithmetic operators
- new and delete operators in C++ for dynamic memory
- Overloading stream insertion (<>) operators in C++
- C | Operators | Question 2
- C | Operators | Question 3