Scriven
Scriven

Reputation: 438

What is the order of operations for compound boolean expressions in an "if" statement

If i have an if statement

if (var1 || var2 && var3)
{
    //anything
}

What is the order it evaluates these variables?

I guess really what I would like to know is if this if statement would be equivalent to

if ((var1 || var2) && var3){}
//or
if (var1 || (var2 && var3)){}

and does order matter in the first instance

I understand I could clarify this with brackets and it would evaluate them in an order I expect. But I was wondering the default way they are viewed when no brackets are present.

Upvotes: 2

Views: 352

Answers (1)

DrewJordan
DrewJordan

Reputation: 5314

From the docs, && has precedence over ||. After precedence is established, then it flows from left to right. So, in your example, it ends up being if (var1 || (var2 && var3)){}.

Upvotes: 4

Related Questions