What's the purpose of wrapping if statements in parentheses?

Ever since I’ve learned how to use Magnitude, there’s been one thing that has always bothered me, what’s the purpose of wrapping if statements in parentheses? This is a vague question, so let me give an example:

if (Position1 - Position2).Magnitude <= Distance then
   ^                     ^

I’m referring to these two parentheses (hence the arrows), what’s the purpose of them?

I’ve also seen people use them in if statements, sorta like this:

if (Something == Something) then

I hope my question is actually understandable, and not alien space language haha, anyway I hope someone can answer my question!

2 Likes

For the first one, it is because of operator precedence. Without the parentheses, you would be doing

Position1 - Position2.Magnitude

You are subtracting Position1 by the magnitude of Position2.

For the last one, they are redundant and are often used out of habit by Java/C++/etc programmers.

In “curly bracket” languages, control structures follow this syntax:

while (condition) {
    // ...
}

if (condition) {
    // ...
}

for (condition) {
    // ...
}

do {
    // ...
} while (condition)
8 Likes

Ah, I see. You’ve just blown my mind, that makes a ton of sense, I appreciate the response, thank you!

1 Like