Double parenthesis on if statement

So I've just watched a video where the guy wrote a if statement like this:
if((minutes < 0) || (seconds < 0) || (seconds > 59))

I didn't know you could write like this and thought this way was the only one:
if(minutes < 0 || seconds < 0 || seconds > 59)

Both works the same,is there any reason to use one instead of the other ?

7 comments · 75% upvoted · other discussions

comments

[deleted]

[deleted]

ColetBrunel

Errm. Can we have an example where they don't yield the same results?

spicycurry55

They gave a bad example

Using && is when things can go awry if you don’t scope the conditional properly

desrtfx

is there any reason to use one instead of the other ?

I would always use the first version - with the parentheses.

  1. Readability - with parentheses it is easier to see the individual parts
  2. Change of operator precedence
  3. Grouping of operations for short circuiting
SantoWest

It depends on your style a bit. I honestly wouldn't ever use the first one. I prefer parenthesis to make the operation priority clear, like when there are both AND and OR statements.

teefj

Right I think in this case the extra parentheses actually makes it harder to read. Even if it’s just a slight difference, anything that makes you do a double take hurts more than it helps.

ColetBrunel

((Why) (would) you (think) (such a (thing)))?

ignotos

To be clear, this isn't a special feature, or a special if-statement, or anything like that.

The condition of an "if" just needs to be any expression which ultimately resolves to true or false.

And an expression can contain practically anything - as long as it's well-formed (i.e. actually makes sense logically / mathematically), it can include whatever brackets, variables, mathematical operations, function calls, etc you decide to use.

Just like in math, you can add brackets either to clarify things, or to actually influence the order in which things are evaluated / how they are grouped. So a * b + c can be written as (a * b) + c if that makes it clearer, or a * (b + c), which will actually change the result. You could even go crazy with unnecessary brackets if you really want to: ((((((a) * b) + (c))))) - that's perfectly legal (although pointless).