简体   繁体   English

是否可以在同一条线上中断?

[英]Is it possible to break on the same line?

Normal way: 正常方式:

switch(whaa){

   case 1:
     if(condition){
       xxx();
       break;
     }

}

but can I break in the same line as my code to avoid the { } ? 但是我可以在代码中插入同一行以避免{}吗?

Like 喜欢

switch(whaa){

   case 1:
     if(condition)
       break xxx();

}

this doesn't work obviously, but perhaps there's a different way? 这显然不起作用,但是也许有另一种方式?

Without the inclusion of brackets, only the first statement gets executed. 如果不包含方括号,则仅执行第一条语句。 So the second statement will execute outside of if . 因此,第二条语句将在if之外执行。

You could however use comma operator to make it a single statement and hence remove the usage of brackets. 但是,您可以使用逗号运算符使它成为单个语句,从而删除方括号的用法。 However, you must focus on quality over quantity . 但是,您必须关注 质量而不是数量

 
 
 
  
  if(condition) xxx(), break;
 
  

What the above does is evaluate to break after executing xxx() in the way. 上面的操作是在执行 xxx()后评估 break

As pointed by thefourtheye, break is a statement and hence can't be used as an expression using comma operator. 正如thefourtheye指出的那样, break是一条语句,因此不能用作使用逗号运算符的表达式。

So it has to be used as an independent statement, so the brackets cannot be removed. 因此,必须将其用作独立语句,因此无法删除方括号。

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

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