Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which side (left or right) of && (and) operator evaluated in C++

Which order is the and && operator evaluated

For example the following code

if (float alpha = value1-value2 && alpha > 0.001)
    //do something

threw an exception that alpha is being used without being initiated. I thought the expression left of the && would always initiate the value of alpha first, but it seems I may be wrong

Any idea?

Thanks

like image 735
zenna Avatar asked Jan 20 '10 16:01

zenna


People also ask

Is the driver side left or right?

Left side is driver side, right side is passenger side. It is important to understand Right Hand vs.

Which side is the right side of a page?

Page numberingPage 1 and all odd-numbered pages are always right-hand pages. Page 2 and all even-numbered pages are always left-hand pages.


1 Answers

This gets parsed as:

if (int alpha = (value1-value2 && (alpha > 0.001)))

... because && has a higher "parsing precedence" than = -- which is probably not what you want. Try:

int alpha = value1-value2; 
if (alpha && (alpha > 0.001))
like image 185
Kornel Kisielewicz Avatar answered Sep 28 '22 12:09

Kornel Kisielewicz