Order of evaluation in operators
Vaman
Posted on September 13, 2020
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.
// 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;
// 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;
// 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;
// conditional (? :) operator
// condition ? expression1: expression2
bLeftOperand ? "operand is true \n" : "operand is false\n"
// only one of the expressions are evaluated
//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.
//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*/
Posted on September 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.