简体   繁体   English

关于C#if-else语法

[英]About c# if-else syntax

In c# you can define an if statement without using braces, like this example 在C#中,您可以不使用花括号就定义一个if语句,例如本例

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

here the this.Exit(); 这是this.Exit(); is the statement associated with the if. 是与if相关联的语句。 But it's not in braces, so my question is, how is it associated with the if? 但这不是大括号,所以我的问题是,它与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? 答案是否仅仅是IDE找到缩进并在编译时自动将其放在花括号中?

The ; ; ends the statement. 结束语句。

Statements (C# Programming Guide) 语句(C#编程指南)

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'语句的语法为:

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. 在您的代码中,由于表达式已关闭并且'this.Exit() '符合语句的语法,因此, is.Exit()调用与if块相关联。

http://ecma-international.org/ecma-262/5.1/ 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. C#和Java中的花括号基本上使多个语句成为一个块集,基本上可以将其理解为特定情况下的作用域。

  • 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. 您可以将1个或多个语句括在花括号中,也可以将该区域留空,无论其中是否有注释。

  • 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 ; 如果在if/else/foreach/for/do/while之后没有打开{ if/else/foreach/for/do/while那么编译器会将任何立即语句视为由其终止的立即语句作为其块的一部分;

  • You can even have no statement after your if/else/foreach/for/do/while if you immediately put a ; 如果您立即输入; if/else/foreach/for/do/while则在if/else/foreach/for/do/while之后甚至都没有语句;

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. 在所有循环中, if-else语句,甚至在switch-case都可以使用花括号将代码放入作用域中。 For me its really very helpful to put the case statements in blocks. 对我来说,将case语句放在块中非常有帮助。 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. 如果在if/else/foreach/for/do/while之后没有开括号{if/else/foreach/for/do/while下一个立即执行语句被视为if/else/foreach/for/do/while块的一部分。
  • 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. 它仅适用于一行,因此,如果要让if语句包含多行,则应使用花括号。

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. 因此,编译器知道,有一个if语句不带花括号,它应该在下一行使用(假设条件为真)。

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. 您可以在if语句的同一行上调用this.Exit(),这仍然可以。 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中的单个Line语句,无需将语句放在大括号中。

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. 在C#中,if语句基于括号运行命令,就像我们使用带括号的if一样。 If no brackets are given, it runs the next command if the statement is true and then runs the command after. 如果没有括号,则在语句为true时运行下一条命令,然后在其后运行命令。 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. 因此,当编译器首先找到它时,它将其包含在IF块中,而不将其包含在IF子句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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