简体   繁体   中英

About c# if-else syntax

In c# you can define an if statement without using braces, like this example

  if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

here the this.Exit(); is the statement associated with the if. But it's not in braces, so my question is, how is it associated with the if?

I learned that the compiler ignores white space, which does not logically make sense in this case. Is the answer simply that the IDE finds the indent and automatically puts it in braces when it compiles?

The ; ends the statement.

Statements (C# Programming Guide)

A statement can consist of a single line of code that ends in a semicolon , or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks.

When your code is parsed by the compiler, it breaks each section into a lexical block. The syntax of the 'if' statement is:

if ( Expression ) Statement else Statement

or

if ( Expression ) Statement

A statement can either be a statement block (ie enclosed in braces) or a single statement. In your code, the th is.Exit() call is associated with the if block by virtue of the fact that the expression has been closed and that 'this.Exit() ' conforms to the syntax of a statement.

http://ecma-international.org/ecma-262/5.1/

The braces in C# and Java are basically make multiple statements a block-set that basically understood as a scope under a particular situation.

  • You can put 1 or more statements in curly braces or leave the area blank no matter if you just have some comments in there.

  • By default compiler seeks every character written in your code, so it goes char by char and when it sees opening { then it expects there must be a closing } . If it finds more opening braces it keeps on counting the code blocks.

  • If there is no opening { after if/else/foreach/for/do/while then compiler considers any immediate statement as part of its block if terminated by a ;

  • You can even have no statement after your if/else/foreach/for/do/while if you immediately put a ;

I have my finding, may be many people already know it or using it, so by the virtue of this question I am putting forward...

  • There can be several uses of { } blocks. In all loops, if-else statements, and even in switch-case you can use braces to put a code in a scope. For me its really very helpful to put the case statements in blocks. If you define a variable in one case, then you cant define it with the same name in another case under same switch... So I use this syntax:

      int abc = 1; switch (abc) { case 1: { var x = 11; } break; case 2: { var x = 11; // its legal. } break; case 3: var x = 11; // its ilegal here too.. because we already have it in previous scope. break; case 4: { var x = 11; // its illegal here because we already have a in the parent/current scope. } break; } 
  • You can also declare variables with same idea:

      ... some code above { var xx = 10; } // xx - is not available as it was declared in the inner-scope { var xx = 11; // Its legal, because its declared in inner-scope. } // xx - is again not available as it was declared in the inner-scope ... some code below 

Summary:

  • If there is no opening brace { after if/else/foreach/for/do/while then the next immediate statement is considered to be the part of if/else/foreach/for/do/while block.
  • You can create as many scopes within your sequential statements to use same variable names.

It only works for one line, so if you want to have an if statement with multiple lines you should use braces.

So, the compiler knows, that is there is an if statement without braces it should use (given that the condition is true) the next line.

No, braces are for multi-line statements. So the white space and indent is irrelevant. You could have the this.Exit() call on the same line as the if statement, and that would still be fine. Some people still prefer braces for single line statements for readability, and that is a matter of choice.

For a single Line statement in if, there is no need to put statement in braces.

If you need to execute more than one statement, braces are required.

In C#, if statements run commands based on brackets as we use if with brackets. If no brackets are given, it runs the next command if the statement is true and then runs the command after. if the condition is false, just continues on the next command.

" ; " is pointed to end of statement/ Termination point. so when compiler found this first it include it in IF block and not include other in IF Clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM