C++循环内的单行 If-Else
我阅读为什么我不需要循环的括号和IF语句,但是我没有足够的声誉点来回复后续问题。
我知道这是不好的做法,但是我受到了挑战,以最大程度地减少我使用的代码行。
您可以在任何版本的C ++中执行此操作吗?
a_loop()
if ( condition ) statement else statement
即if/else块是否将其视为一个“语句”?
同样,如果/else如果.../else算作一个“语句”吗?尽管这样做将变得完全不可读。
我上面提到的帖子只说了类似的话:
a_loop()
if(condition_1) statement_a; // is allowed.
I read Why I don't need brackets for loop and if statement, but I don't have enough reputation points to reply with a follow-up question.
I know it's bad practice, but I have been challenged to minimise the lines of code I use.
Can you do this in any version of C++?
a_loop()
if ( condition ) statement else statement
i.e. does the if/else block count as one "statement"?
Similarly, does if/else if.../else count as one "statement"? Though doing so would become totally unreadable.
The post I mentioned above, only says things like:
a_loop()
if(condition_1) statement_a; // is allowed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
三元运算符
而不是如果... else
,而如果其参数的值始终是
true
,则在此处看起来多余/code>因此您可以简单地写You can use
ternary operator
instead ofif...else
while
seems redundant here if the value of its argument is alwaystrue
so you can simply write是的,从句法上看,您可以做到这一点。
如果/else block是选择statement ,这是一种陈述。
Yes, syntactically you can do that.
if/else block is a selection-statement, which is a kind of statement.
N3337 6.4 Selection statements says: