-5

I read a piece of code written as

for (kf=0; kf<nf; kf++)
    if (EPS_MOCK[kf] == 1)
        for (i=0; i<nptsx; i++)
            for (j=0; j<nptsz; j++)
            {
                x0[iv] = log(inv_Controls->epsilonBed.GetElem(j,i,kf));
                iv = iv+1;
            }
for (kf=0; kf<nf; kf++)
    if (inv_num_packman[kf] == -1)
        for (i=0; i<nzx; i++)
        {
            x0[iv] = log(inv_Controls->num_packman[i+kf*nzx]);
            iv = iv+1;
        }
    else if (inv_num_packman[kf] == 1)
    {
        x0[iv] = log(inv_Controls->num_packman[kf*nzx]);
        iv = iv+1;
    }

Whereas intended parenthesis settings would be

for (kf=0; kf<nf; kf++){
    if (EPS_MOCK[kf] == 1){
        for (i=0; i<nptsx; i++){
            for (j=0; j<nptsz; j++)
            {
                x0[iv] = log(inv_Controls->epsilonBed.GetElem(j,i,kf));
                iv = iv+1;
            }
        }
    }
}
for (kf=0; kf<nf; kf++){
    if (inv_num_packman[kf] == -1){
        for (i=0; i<nzx; i++){}
        {
            x0[iv] = log(inv_Controls->num_packman[i+kf*nzx]);
            iv = iv+1;
        }
    }
    else if (inv_num_packman[kf] == 1)
    {
        x0[iv] = log(inv_Controls->num_packman[kf*nzx]);
        iv = iv+1;
    }
}

Is the first version correct ? Are parenthesis here only for better lisibility or mandatory for compiler to understand the code's logic ?

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • Really you should aim to write the most readable code possible. The first form while equivalent to the second will be less readable for many people. – shuttle87 Jun 09 '14 at 02:05

2 Answers2

5

First of all, what you are talking about is not parenthesis, its curly braces.

for (kf=0; kf<nf; kf++)
    if (EPS_MOCK[kf] == 1)
        for (i=0; i<nptsx; i++)
            for (j=0; j<nptsz; j++)

Compiler will definitely support this type of syntax, but it will be confusing, to make your code more readable, follow simple rule, if you are not dealing with more that one line, don't use curly braces, otherwise use curly braces.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • 6
    _'if you are not dealing with more that one line, don't use curly braces'_ I disagree with this advice. You should always use a block, same for `if()` statements. – πάντα ῥεῖ Jun 08 '14 at 07:33
  • That may also true. But I think that depends on style of coding. – Pranit Kothari Jun 08 '14 at 07:36
  • @πάνταῥεῖ why do you advice using blocks also when one line only ? – kiriloff Jun 08 '14 at 08:38
  • 1
    @antitrust Because it's error prone when the code is maintained and changed in the future otherwise. There's absolutely nothing saved by omitting these two `{}`. (Think about Apple's famous `goto fail;` bug recently) – πάντα ῥεῖ Jun 08 '14 at 08:40
5

An if or for applies to a single immediately following statement. That can be any one statement, but more commonly is a block enclosed in {} braces. The braces turn a group of statements into a single statement.

Although any single statement can be used, in the opinions of many programmers including myself, it is generally clearer to always use a brace-enclosed block, even if the block only contains one statement.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75