需要括号吗? [英] braces needed?

查看:77
本文介绍了需要括号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近的编程如果分支只应用于单行,我就习惯于在

语句中省略括号,假设它将

全部工作并且是相当于if(condition){statement; }"这些算法实现了

算法并且它暂时不起作用,当我跟踪问题

时,就好像某个动作没有完成其中一个如果

没有括号的语句,那么我将所有if语句放在

括号中,并且它按预期工作。这不是由于我的误解而导致的b / b
,如果我知道

缩进并不重要,这些陈述通常都在一个线路

无论如何。


当我第一次开始这样做时似乎工作正常,但是当有的时候没有

a一行或彼此靠近,而环路等等。在我的代码的一些部分中,我忽略了将它们更改为括号而没有任何

问题,让我感到困惑。


我有时也会觉得想要在while和for循环中省略大括号的愿望

,因为我发现它们很烦人并喜欢缩短我的代码。我能否

避免括号?是保证工作还是做不同的编译器

产生不同的结果?如果我现在有完美的代码和所有

我做的是删除单个语句中的大括号if,for和while循环,是否

保证没有任何区别或是我天真地对其他一些潜在的

问题?


感谢您的澄清。

解决方案

2004年1月31日星期六02:02:04 +0000(UTC)comp.lang.c ++,Jason

< ja ********* **@btinternet.com>据称写了:

保证工作或做不同的编译器会产生不同的结果吗?如果我现在的代码完全正常,那么我所做的就是从单个语句中删除大括号,如果,for和while循环,它是否保证没有区别,或者我对其他潜在的东西是天真的/>问题?




保证在所有编译器上都能正常工作。

语言语法的这一部分从C的开始就已经很好地定义了,并且

找到错误的编译器将是一个巨大的冲击。 br />

由if控制的东西或while概念上是单个

语句。带有大括号的多个语句被认为是单个复合语句。

单个复合语句。如果你愿意,你可以在一个与任何if无关的

语句组中加括号。或while。


我更喜欢省略if的冗余括号。或while它控制了一个

单一陈述,但每次使用它们都没有什么特别的错误

。有人说它可以减少出错的可能性,并且可能会根据您的经验确定
。我发现没有它们可以更好地阅读

代码,但当然你必须理解语言

规则。




David Harmon <所以**** @ netcom.com>在消息中写道

新闻:40 *************** @ news.west.earthlink.net ...

周六,2004年1月31日02:02:04 +0000(UTC)in comp.lang.c ++,Jason
< ja *********** @ btinternet.com>据称写了:

保证工作或做不同的编译器会产生不同的结果吗?如果我现在有完美的代码和
allI do是从单个语句中移除大括号if,for和while循环,是
它保证没有区别或者我对其他潜力是天真的
问题?
保证在所有编译器上都能正常工作。这部分语言语法从一开始就已经很好地定义了,并且找到一个错误的编译器将是一个巨大的震撼。

由if控制的东西或while概念上是一个单一的声明。带有大括号的多个语句被认为是单个复合语句。如果你愿意,你可以在一组与任何if无关的语句中加上大括号。或while。

我更喜欢省略if的冗余括号。或while它控制着一个单一的声明,但每次使用它们都没有什么特别的错误。有人说它减少了出错的可能性,并且根据你的经验可能确实如此。我发现在没有它们的情况下阅读
代码会更好,但当然你必须理解语言
规则。




感谢您的回复(grr @ dig :))。我从来没有看过语法规则

我使用过的编程语言,我刚刚用过它,但现在我是

有兴趣,任何在线参考c ++的完整语法,所以我可以

了解所有的语言规则?所有我得到的谷歌都是热门广告

的书籍。



2004年1月31日星期六02: 02:04 +0000(UTC),Jason

< ja *********** @ btinternet.com>在comp.lang.c中写道:++:

最近的编程如果分支只应用于单行,我就习惯于在if语句中省略括号,假设它将
全部工作并等同于if(condition){声明; }"我实现了一个算法,它暂时没有工作,当我跟踪问题时,就好像某个动作没有从其中一个那样做,如果
语句做了没有括号,所以我将所有if语句放在
大括号中,并按预期工作。这不是我的误解的结果,例如包括2个无支撑下的陈述如果我知道
缩进并不重要,这些陈述通常都在一条线上
无论如何。

当我第一次开始这样做时它看起来工作正常,但是当没有连续几个或彼此靠近时,环路等等。在我的代码的一些部分中,我忽略了将它们更改为括号而没有任何问题,让我感到困惑。

我有时也会感觉到想要省略括号的愿望。循环
因为我觉得它们很烦人,喜欢缩短我的代码。我能否
避免括号?是保证工作还是做不同的编译器会产生不同的结果?如果我现在的代码完全正常,那么我所做的就是从单个语句中删除大括号,如果,for和while循环,它是否保证没有区别,或者我对其他潜在的东西是天真的问题?

感谢您的澄清。




没有向我们展示一些行为不正确的代码,以及什么

你改变它以使它按照你想要的方式运行,这是很难说肯定出了什么问题。很可能你有一个

" else"或elseif或匹配到错误的if。


我从来没有见过一个专业的编码标准,不需要

围绕所有受控条款的括号,甚至如果他们只是一个单一的声明。在我工作的地方也不会有任何地方我可以用b $ b来控制编码标准。


错误的可能性太大了,不仅在编写

原始代码时,而且在代码修改为

缺陷修复或升级时更是如此。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c ++ http://www.parashift .com / c ++ - faq-lite /

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc。 HTML


Programming recently I was the habit of leaving out the braces on if
statements if the branch only applied to a single line, assuming it would
all work and be equivalent to "if(condition) { statement; }" I implemented
an algorithm and it did not work for a while and when i tracked the problem
down it was as if a certain action was not being done from one of these if
statements that did not have a brace, so I placed all if statements in
braces and it worked as expected. This was not a result of misunderstanding
on my part such as including 2 statements under the braceless if and I know
indentation is not important, the statements were usually all on one line
anyhow.

It appeared to work ok when I first started doing it, but not when there was
a few in a row or near each other inside while loops and such like. In some
parts of my code I have neglected to change them to braces without any
problems, confusing me.

I also sometimes feel the desire to omit braces from while and for loops
because i find them irritating and like to shorten my code. Am I able to
avoid braces or not? Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?

thanks for clarifying.

解决方案

On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<ja***********@btinternet.com> was alleged to have written:

Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?



It is guaranteed to work the same on all compilers. This part of the
language syntax has been well defined from the very beginning of C, and
it would be a huge shock to find a compiler that got it wrong.

The thing controlled by an "if" or "while" is conceptually a single
statement. Multiple statements with braces around them are considered a
single "compound statement". If you want, you can put braces around a
group of statements that have nothing to do with any "if" or "while".

I prefer to omit redundant braces for an "if" or "while" that controls a
single statement, but there is nothing terribly wrong about using them
every time. Some people say it reduces the likelihood of error, and
based on your experience maybe it does. I find it nicer to read the
code without them, but of course you have to understand the language
rules.



"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...

On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<ja***********@btinternet.com> was alleged to have written:

Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and allI do is remove braces from single statement if, for and while loops, is itguaranteed to make no difference or am I naive to some other potential
problem?
It is guaranteed to work the same on all compilers. This part of the
language syntax has been well defined from the very beginning of C, and
it would be a huge shock to find a compiler that got it wrong.

The thing controlled by an "if" or "while" is conceptually a single
statement. Multiple statements with braces around them are considered a
single "compound statement". If you want, you can put braces around a
group of statements that have nothing to do with any "if" or "while".

I prefer to omit redundant braces for an "if" or "while" that controls a
single statement, but there is nothing terribly wrong about using them
every time. Some people say it reduces the likelihood of error, and
based on your experience maybe it does. I find it nicer to read the
code without them, but of course you have to understand the language
rules.



Thanks for the reply(grr @ dig:)). I''ve never looked at the syntax rules
for a programming language I''ve used, I''ve just used it, but now I''m
interested, any online references to the complete syntax of c++ so I can
understand all the language rules? all i get on google is fricking adverts
for books.



On Sat, 31 Jan 2004 02:02:04 +0000 (UTC), "Jason"
<ja***********@btinternet.com> wrote in comp.lang.c++:

Programming recently I was the habit of leaving out the braces on if
statements if the branch only applied to a single line, assuming it would
all work and be equivalent to "if(condition) { statement; }" I implemented
an algorithm and it did not work for a while and when i tracked the problem
down it was as if a certain action was not being done from one of these if
statements that did not have a brace, so I placed all if statements in
braces and it worked as expected. This was not a result of misunderstanding
on my part such as including 2 statements under the braceless if and I know
indentation is not important, the statements were usually all on one line
anyhow.

It appeared to work ok when I first started doing it, but not when there was
a few in a row or near each other inside while loops and such like. In some
parts of my code I have neglected to change them to braces without any
problems, confusing me.

I also sometimes feel the desire to omit braces from while and for loops
because i find them irritating and like to shorten my code. Am I able to
avoid braces or not? Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?

thanks for clarifying.



Without showing us some of the code that behaved incorrectly, and what
you changed it to to make it behave the way you wanted it to, it is
difficult to say for sure what went wrong. Most likely you had an
"else" or "elseif" matched up to the wrong "if".

I have never seen a professional coding standard that did not require
the braces around all controlled clauses, even if they were only a
single statement. Nor will there ever be one anyplace I work where I
have any measure of control over the coding standard.

There is just too much chance for error, not only when writing the
original code, but even more so later when the code is modified for
defect fixing or upgrade.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于需要括号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆