lundi 18 mai 2015

Why does this if statement require brackets?

Usually, if I follow an if statement with a single statement, I won't need brackets. For example:

if(blah) statement1; statement2;

statement2 will run in any case. But that doesn't happen here:

for(j=0; j<size; j++{
    if(size%2) if(j%2) *(minmat+j) *= -1.0;        
    else {
        .
        .
        .
    }
}

The else statement is supposed to associate with the first if statement, but it in fact associates with the second. To correct it, I have to do this:

for(j=0; j<size; j++{
    if(size%2){ if(j%2) *(minmat+j) *= -1.0; }
    else {
        .
        .
        .
    }
}

But why does that happen, when in the first case it's 'implied' that the second if statement is inside brackets?

Aucun commentaire:

Enregistrer un commentaire