If - Else If - Else Statement and Brackets

if - else if - else statement and brackets

R reads these commands line by line, so it thinks you're done after executing the expression after the if statement. Remember, you can use if without adding else.

Your third example will work in a function, because the whole function is defined before being executed, so R knows it wasn't done yet (after if() do).

else if & curly brackets

If you format correctly the first code snippet

while (1){
if (i>=n&&j<0)
break;

else if (j<0)
if (Arr[i])
c++;

else if (i>=n)
if(Arr[j])
c++;
else if (Arr[i]==1&&Arr[j]==1)
c+=2;

i++;
j--;
}

then it is seen that else or else if correspond to the closest if statement.

Is it a bad practice to use an if-statement without curly braces?

The problem with the first version is that if you go back and add a second statement to the if or else clauses without remembering to add the curly braces, your code will break in unexpected and amusing ways.

Maintainability-wise, it's always smarter to use the second form.

EDIT: Ned points this out in the comments, but it's worth linking to here, too, I think. This is not just some ivory-tower hypothetical bullshit: https://www.imperialviolet.org/2014/02/22/applebug.html

Curly braces in if-else blocks


For example:

if (something)
if (something else)
...
else (blah blah)

Is the same that:

 if (something)
{
if (something else)
...
else (blah blah)
}

In this case, yes, they're the same, but the former is harder to read than the latter. I recommend using braces always, even for blocks of code made of a single line.

Find all if else and else if statements with no parentheses

One problem you are facing is that the regex is matching as much as it can on the .* to find a pattern match. Therefore, and depending on the options used (e.g. . matches anything but \n, or anything), you'll get unsatisfactory results.

Another problem is that you'd need to match recursively, e.g. skip as many ) as there were nested '(' in the expression. Only a very few regex engines can do this; .NET fortunately can via "balancing groups" but it is tricky and highly advanced application of regex. Also, for that to work correctly, you'd have to also recognize string and character literals (in quotes) as to not count parens in these.

Edit This regex for .NET should pretty reliably find these if and else statements:

\b(if\s*\(((?:[^()"']|'(\\.|[^'\\])'|"(\\.|[^"\\])"|(?<parens>\()|(?<-parens>\)))*(?(parens)(?!))\))|else)\s*[^{\s]

While this shows how powerful regexes can be, it's very cryptic and the proper way to do this would really be with a real parser (such as Roslyn).

Formatting of if Statements

I find this:

if( true ) {
DoSomething();
} else {
DoSomethingElse();
}

better than this:

if( true )
DoSomething();
else
DoSomethingElse();

This way, if I (or someone else) comes back to this code later to add more code to one of the branches, I won't have to worry about forgetting to surround the code in braces. Our eyes will visually see the indenting as clues to what we're trying to do, but most languages won't.

Are brackets required at the end for if-else statements?


Is this a new php standard?

Not really. It totally depends on the user. You can use it in your code if you want -- it's your coding style. However, the Zend Coding standard requires you to use braces for all if, else, elseif constructs:

From their documentation:

PHP allows statements to be written without braces in some circumstances. This coding standard makes no differentiation- all "if", "elseif" or "else" statements must use braces.


I understand that not using brackets can make it a little more confusing, but I think in some cases it looks elegant

It's a good idea to use braces if your if blocks includes multiple conditions. Say, for example:

if ($auth) {
doStuff();
anotherOne();
}

However, if your code only has a single condition, you may write it without braces. For example:

if(is_numeric($n)) echo 'Foo';

For very short statements, it's not necessary to use braces, but if your code involves multiple conditions or you're writing code that another developer is going to re-use, then it's best to wrap them in braces. That way, there will not be any confusion when you look at the code in future.



Related Topics



Leave a reply



Submit