Order of evaluation in operators
Tags: beginners, cpp, javascript, python
Published: 2020-09-13 15:47:22

---
title: Order of evaluation in operators
published: true
description: 
tags: beginners, cpp, javascript, python
//cover_image: https://direct_url_to_image.jpg
---
I thought of just sharing about the order of evaluation of operand using operators. As a beginner there’s always confusion arises regarding the order of evaluation...
There are 4 operators which I will discuss. 
AND (&&) , OR (||), Conditional (? :) and comma (,) operator.

All the above-mentioned operators evaluated from left to right. And these operator guarantees that left-hand operand is evaluated first. And also, the right-hand operand is evaluated only if the left-hand operand does not determine the result.

```cpp
// Variables used
bool bLeftOperand = false, bRightOperand = true;
int iLeftOperand  = 100, iRightOperand = 0;
struct TempStruct
{
    int m_iCount;
    string m_sName;
    TempStruct():m_iCount(0){} // member initialization
};
TempStruct *stTempPtr = NULL;
```
```cpp
// AND (&&) Operator
// If left side operand is false right side will not be evaluated
if (bLeftOperand && bRightOperand)
    cout << "Both operands are true"<<endl;
else
    cout << "bLeftOperand operand is false but bRightOperand is true. Else case is executed based on order of evaluation"<< endl;

if (iLeftOperand && iRightOperand)
    cout << "Both operands are true"<<endl;
else
    cout << "iLeftOperand operand is true but iRightOperand is false. Else case is executed because iRightOperand is false" <<endl;

// Although stTempPtr is null pointer program will not crash during execution because of order of evaluation
if (stTempPtr && stTempPtr->m_iCount)
    cout << "Struct stTempPtr is valid pointer" <<endl;
else
    cout << "Struct stTempPtr is a NULL pointer" <<endl;
```

```cpp
// OR (||) operator
// If left side operand is true right side will not be evaluated
if (bLeftOperand || !bRightOperand)
    cout << "Either of the operands are true"<<endl;
else
    cout << "both operands are false"<< endl;
    
if (iLeftOperand || iRightOperand)
   cout << "Either of the operands are true"<<endl;
else
   cout << "iLeftOperand operand is true but iRightOperand is false. Else case is executed because iRightOperand is false" <<endl;

if (stTempPtr)
   cout << "Struct stTempPtr is valid pointer" <<endl;
else
   cout << "Struct stTempPtr is a NULL pointer" <<endl;
```

```cpp
// conditional (? :) operator
// condition ? expression1: expression2

bLeftOperand ? "operand is true \n" : "operand is false\n"
// only one of the expressions are evaluated
```

```cpp
//comma operator (,) used to separate two or more expressions
// only the right-most expression is considered.

int b;
int a = (b=3, b+2)

//would first assign the value 3 to b, and then assign b+2 to
//variable a. So, at the end, variable a would contain the value 5
//while variable b would contain value 3.
```

**Shortcircuit Evaluation:** Term used to describe how the logical AND and logical OR operators execute. If the first operand to these operators is sufficient to determine the overall result, evaluation stops. We are guaranteed that the second operand is not evaluated.

```cpp
//Example of a short circuit
int x=20,y=40,z=60;

 if(x<y && ++y<z)   

cout<<x<<" "<<y<<" "<<z;   

else 

cout<<x<<" "<<y<<" “<<z;   
/* The output will be 
20 40 60*/
```