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
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 + ccan be written as(a * b) + cif that makes it clearer, ora * (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).