Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# support if codeblocks without braces?

Tags:

How would C# compile this?

if (info == 8)     info = 4; otherStuff(); 

Would it include subsequent lines in the codeblock?

if (info == 8) {     info = 4;     otherStuff(); } 

Or would it take only the next line?

if (info == 8) {     info = 4; } otherStuff(); 
like image 298
Kevin Boyd Avatar asked Dec 03 '10 13:12

Kevin Boyd


People also ask

Does C have do-while?

The C do while statement creates a structured loop that executes as long as a specified condition is true at the end of each pass through the loop.

What is || in C programming?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .


2 Answers

Yes, it supports it - but it takes the next statement, not the next line. So for example:

int a = 0; int b = 0; if (someCondition) a = 1; b = 1; int c = 2; 

is equivalent to:

int a = 0; int b = 0; if (someCondition) {     a = 1; } b = 1; int c = 2; 

Personally I always include braces around the bodies of if statements, and most coding conventions I've come across take the same approach.

like image 95
Jon Skeet Avatar answered Oct 11 '22 05:10

Jon Skeet


if (info == 8) {     info = 4; } otherStuff(); 
like image 39
dejanb Avatar answered Oct 11 '22 03:10

dejanb