I have seen different ways of writing an if
statement.
Which one do you prefer and why?
Example 1:
if (val % 2 == 1){output = “Number is odd”;}else{output = “Number is even”;}
Example 2:
if (val % 2 == 1)
{
output = “Number is odd”;
}
else
{
output = “Number is even”;
}
Example 3:
if (val % 2 == 1)
output = “Number is odd”;
else
output = “Number is even”;
Example 4:
if (val % 2 == 1){
output = “Number is odd”;
} else {
output = “Number is even”;
}
None of the above.
If my execution block only has one line (even if it's a huge for statement) then I don't use braces, but I do indent it, similar to #3
An example with multiple statements that do not need curly braces:
That said, Jon Skeet's response in this question is the best
I prefer 4 myself, but I think 2 is definitely good too.