# Developers Planet

daramasala February 2016

### Why do many languages use parentheses around `if` condition

Many programming languages require parentheses around the if condition.

For example:

if (x < 50)
{
}

Why can't it be written like this, without the parentheses:

if x < 50
{
}

Assuming that language designers are pragmatic people, why are the parentheses required?

Marc B February 2016

Simple answer: operator precedence. Remember basic highschool/elementary math: What's the answer for 7 * 4 + 3? 31? or 49?

Braces allow you to impose your OWN precedence, to override what the language's natural precedence would be:

(7 * 4) + 3 -> 31 (the natural answer by BEDMAS rules)
7 * (4 + 3) -> 49

If you're talking about the {}, then that's to allow a multi-line block for the expression.

No braces:

if (...)
a = 1;  // only this line is part of the "if"
b = 1;  // ignore the indentation - this is always executed

v.s.

if (...) {
a = 1;  // part of the "if"
b = 1;  // also part of the "if"
}

Archie February 2016

In C, if takes actually a comma-operator, instead of just an expression. One can write:

int i = 0;
if (i = i + 1, i/2 > 0) {
}

So you need braces (parenthesis actually) here. The same is true for while() for example.

return operator in C however accepts just an expression, so it does not require parenthesis:

return i + 1;

though many programmers still write it like:

return (i + 1);

#### Post Status

Asked in February 2016
Viewed 3,866 times
Voted 6