Should I keep doing this player iteration separation in if statements?

I’m not really sure where to put this but i though that code review would be the best fit

I am starting to use () in some If statements and i’m wondering if i should keep using them like this

Example:

	if mynumber == 1 and (#players == 1 or randomNumber == 1) then

If it’s comfortable to you, and consistent across your code. Stick with it. All my if statements are written like i was using other languages. if (Variable1 == (Variable2 + Variable3)) then

Well it depends, parentheses directly change the flow of the code. For example:
vumq5c_94201

As you can see x and y or z is not the same as x and (y or z)

1 Like

Okay

I didn’t see anybody really do it like this so I just didn’t really know if doing this is a good idea

What’s your exact use case? What are you trying to accomplish here with these evaluations?

It’s not required in lua, but I had also forgotten to mention what @steven4547466 mentioned where the code could be changed with them.

I used them to seperate multiple interperations(Not really sure if i’m saying this right)

This if statement can mean:

	if mynumber == 1 and #players == 1 or randomNumber == 1 then

if mynumber == 1 AND #players == 1 or randomNumber == 1

or

if mynumber == 1 and #players == 1 OR randomNumber == 1

Operator precedence is a thing. The and operator has higher precedence over or. So a and b or c is the same as (a and b) or c. There is no ambiguity without parentheses. It works the same way like in English.

“You can get a slice of pizza or a hamburger and fries.”

Oh okay!

I didn’t know there was an operator precendence, but i still wonder if it is possible to have the opposite:

“You can get a slice of pizza and a hamburger or fries.”

Yes, a and (b or c) accomplishes that. Also another thing, just in case, binary logical operators are left associative, so if you have several ands chained, they will be evaluated from left to right, so a and b and c is the same as (a and b) and c, this is true for any operators with the same precedence that are left associative. There are also right associative operators, namely concatenation and exponentiation. a^b^c is the same as a^(b^c), for example.

Great!

Thanks a lot! This really helped me understand how if statements work