在C ++中为什么不能这样写一个for()循环:for(int i = 1,double i2 = 0; [英] In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0;

查看:268
本文介绍了在C ++中为什么不能这样写一个for()循环:for(int i = 1,double i2 = 0;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或在for循环中声明多个变量ist verboten?



我的原始代码是

  for(int i = 1,int i2 = 1; 
i2 i ++,i2 = i * i){

我想循环遍历第一个这么多的正方形,想要数字和它的平方,在广场上。这个代码似乎是意图的最干净的表达,但它是无效的。我可以想到十几种方法来解决这个问题,所以我不是寻找最好的替代品,而是为了更深入地了解为什么这是无效的。有些语言律师,如果你愿意。



我已经足够记得当你必须在函数开始时声明所有的变量,所以我欣赏

  for(int i = 0; .... 

语法读取它看起来像在for()语句的第一部分只能有一个类型声明,所以你可以做



  for(int i = 0,j = 0; ... 

甚至轻微的巴洛克

  for(int i = 0 ,* j =& i; ... 

但不是to-me-sensible



  for(int i = 0,double x = 0.0; ... 
pre>

有人知道为什么吗?这是for()的限制吗?还是对逗号列表的限制,例如逗号列表的第一个元素可以声明一个类型,但不是其他?以下是使用逗号的不同句法元素的C ++?



(A)

  for(int i = 0,j = 0; ... 

(B)

  int i = 0,j = 0; 

(C)

  int z; 
z = 1,3,4;

任何古茹在那里?



=================================== =================



根据我收到的良好回应,我想我可以提出这个问题:



在for语句

  for(X; Y; ​​Z ;){.....} 

什么是X,Y和Z?



我的问题是关于C ++,但我没有一个伟大的C ++反驳。在我的C参考(Harbison和Steele 4th ed,1995)中,它们都是三个表达式,而我的gcc需要C99模式用于(int i = 0;



在Stroustrup,第6.3节中,for语句语法以



为(for-init-statement; condition; p>

所以C ++有一个特殊的句法语句专门用于for()的第一个子句,我们可以假设它们有超出表达式的特殊规则。 / p>

解决方案

int i = 1,double i2 = 0; 有效的声明语句,因此它不能在中用于语句。如果语句不能独立于之外,则它不能在中用于语句。



编辑:
关于逗号运算符的问题,选项'A'和'B'是相同的,都是有效的。选项'C'也是有效的,但可能不会做你期望的。 z 将被分配 1 ,语句 3 4 实际上不做任何事情(你的编译器可能会警告你关于无效的语句并优化它们)。



更新:
要解决编辑中的问题,请参阅 C ++ spec (第6.5节)为定义

  for(for-init语句条件(opt); expression(opt))语句
pre>

它还将 for-init-statement 定义为 expression-statement 简单声明条件表达式是可选的。



for-init-statement 可以是任何有效的表达式语句(例如 i = 0; )或简单声明(例如 int i = 0; ) 。根据规范,语句 int i = 1,double i2 = 0; 不是有效的简单声明因此对使用无效。作为参考,简单声明(在第7节中)定义为:

  attribute-specifier(opt)decl-specifier-seq(opt)init-declarator-list(opt); 

其中 decl-specifier-seq 数据类型加上 static extern init-declarator-list 将是逗号分隔的声明符列表及其可选的初始化器。尝试在同一个简单声明中放置多个数据类型基本上会在 decl-specifier-seq 编译器期望一个 init-declarator-list 。看不到这个元素会导致编译器将该行视为错误的。



该规范还注意到 for loop等价于:

  {
for-init语句
while {
statement
expression;
}
}

其中条件如果省略,默认为true。考虑这种扩展形式可能有助于确定给定的语法是否可以用于 for 循环。


or, "Declaring multiple variables in a for loop ist verboten" ?!

My original code was

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i2 = i * i ) {

I wanted to loop through the first so-many squares, and wanted both the number and its square, and the stop condition depended on the square. This code seems to be the cleanest expression of intent, but it's invalid. I can think of a dozen ways to work around this, so I'm not looking for the best alternative, but for a deeper understanding of why this is invalid. A bit of language lawyering, if you will.

I'm old enough to remember when you had to declare all your variables at the start of the function, so I appreciate the

for( int i = 0; ....

syntax. Reading around it looks like you can only have one type declaration in the first section of a for() statement. So you can do

for( int i=0, j=0; ...

or even the slightly baroque

for( int i=0, *j=&i; ...

but not the to-me-sensible

for( int i=0, double x=0.0; ...

Does anyone know why? Is this a limitation of for()? Or a restriction on comma lists, like "the first element of a comma list may declare a type, but not the other? Are the following uses of commas distinct syntactical elements of C++?

(A)

for( int i=0, j=0; ...

(B)

int i = 0, j = 0;

(C)

 int z;
 z = 1, 3, 4;

Any gurus out there?

====================================================

Based on the good responses I've gotten, I think I can sharpen the question:

In a for statement

for( X; Y; Z;) {..... }

what are X, Y and Z?

My question was about C++, but I don't have a great C++ refrence. In my C reference (Harbison and Steele 4th ed, 1995), they are all three expressions, and my gcc requires C99 mode to use for( int i = 0;

In Stroustrup, sec 6.3, the for statement syntax is given as

for( for-init-statement; condition; expression ) statements

So C++ has a special syntactic statement dedicated to the first clause in for(), and we can assume they have special rules beyond those for an expression. Does this sound valid?

解决方案

int i = 1, double i2 = 0; is not a valid declaration statement, so it cannot be used inside the for statement. If the statement can't stand alone outside the for, then it can't be used inside the for statement.

Edit: Regarding your questions about comma operators, options 'A' and 'B' are identical and are both valid. Option 'C' is also valid, but will probably not do what you would expect. z will be assigned 1, and the statements 3 and 4 don't actually do anything (your compiler will probably warn you about "statements with no effect" and optimize them away).

Update: To address the questions in your edit, here is how the C++ spec (Sec 6.5) defines for:

for ( for-init-statement condition(opt) ; expression(opt) ) statement

It further defines for-init-statement as either expression-statement or simple-declaration. Both condition and expression are optional.

The for-init-statement can be anything that is a valid expression-statement (such as i = 0;) or simple-declaration (such as int i = 0;). The statement int i = 1, double i2 = 0; is not a valid simple-declaration according to the spec, so it is not valid to use with for. For reference, a simple-declaration is defined (in Section 7) as:

attribute-specifier(opt) decl-specifier-seq(opt) init-declarator-list(opt) ;

where decl-specifier-seq would be the data type plus keywords like static or extern and init-declarator-list would be a comma-separated list of declarators and their optional initializers. Attempting to put more than one data type in the same simple-declaration essentially places a decl-specifier-seq where the compiler expects a init-declarator-list. Seeing this element out of place causes the compiler to treat the line as ill-formed.

The spec also notes that the for loop is equivalent to:

{
    for-init-statement
    while ( condition ) {
        statement
        expression ;
    }
}

where condition defaults to "true" if it is omitted. Thinking about this "expanded" form may be helpful in determining whether a given syntax may be used with a for loop.

这篇关于在C ++中为什么不能这样写一个for()循环:for(int i = 1,double i2 = 0;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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