C++ 无法理解带有两个变量的 for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19194436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot understand for loop with two variables
提问by Arun
When I use two variables in a for loop with different conditions two conditions like I have used below i<3,j<2
the for loop is always executing till the second condition fails.
当我在具有不同条件的 for 循环中使用两个变量时,我在 for 循环下面使用的两个条件i<3,j<2
总是执行,直到第二个条件失败。
#include<iostream>
#include<conio.h>
using namespace std ;
int main()
{
int i,j ;
for(i=0,j=0;i<3,j<2;i++,j++)
{
cout<<"hello" ;
}
getch() ;
return 0 ;
}
In that code, hello
is printed 2 times. Why?
在该代码中,hello
打印了 2 次。为什么?
If I use i<3,j<10
, "Hello" is printed 10 times. I can't understand why the first condition is being neglected. Is it compiler dependent or something else?
如果我使用i<3,j<10
,“Hello”将打印 10 次。我不明白为什么第一个条件被忽略了。它是依赖于编译器还是其他什么?
Every thing works normal if I replace with conditions like || (OR) or &&(AND).An other thing is that I cannot initialize i and j in the for loop itself, it is showing me an error, but works fine when I declare variables in C style or one variable outside the for loop, why is it so?
如果我替换为 || 之类的条件,则一切正常 (OR) 或 &&(AND)。另一件事是我无法在 for 循环本身中初始化 i 和 j,它向我显示一个错误,但是当我以 C 样式声明变量或在 for 循环之外声明一个变量时工作正常,为什么会这样?
Compiler I have used is Orwell Dev C++.
Thanks in advance.
我使用的编译器是 Orwell Dev C++。
提前致谢。
回答by P0W
for(i=0,j=0;i<3,j<2;i++,j++)
for(i=0,j=0;i<3,j<2;i++,j++)
is equivalent to
相当于
for(i=0,j=0;j<2;i++,j++)
for(i=0,j=0;j<2;i++,j++)
The comma expression takes on the value of the last expression.
逗号表达式采用最后一个表达式的值。
Whichever condition is first, will be disregarded, and the second one will be used only.
无论是第一个条件,都将被忽略,仅使用第二个条件。
回答by CygnusX1
The for
loop consists of:
该for
循环包括:
for(START_STATEMENT; CONDITION_EXPRESSION, LOOP_EXPRESSION) BODY_BLOCK
for(START_STATEMENT; CONDITION_EXPRESSION, LOOP_EXPRESSION) BODY_BLOCK
Where:
在哪里:
START_STATEMENT
is any singlestatement, which may include variable declaration. If you want to declare 2 variables, you can writeint i=0, j=0
, but notint i=0; int j=0
because the latter are actually 2 statements. Also node, that variable declaration is a part of statement, but cannot be a part of (sub) expression. That is whyint i=0, int j=0
would also be incorrect.CONDITION_EXPRESSION
is any singleexpression that evaluates to a boolean value. In your case you are using a coma operatorwhich has the following semantics:A, B
will do:- evaluate A (it will evaluate, not just ignore)
- ditch the result of A
- evaluate B
- return B as the result
In your case:
i<3,j<2
you arecomparingi<3
, you are just ignoring the result of this comparison.Comma expressions are useful when the instructions have some side effects, beyond just returning a value. Common cases are: variable increment/decrement or assignment operator.
LOOP_EXPRESSION
is any singleexpression that does not have to evaluate to anything. Here you are using the comma expression again, ignoring the result of the left-hand-side. In this case however, you are not using the result anyway, and just using the ++ side effect - which is to increment the values of your variables.BODY_BLOCK
is either a single statement or a block, encapsulated with curly braces.
START_STATEMENT
是任何单个语句,其中可能包括变量声明。如果要声明 2 个变量,可以写int i=0, j=0
,但不是int i=0; int j=0
因为后者实际上是 2 个语句。同样节点,该变量声明是 statement 的一部分,但不能是 (sub) expression的一部分。这就是为什么int i=0, int j=0
也会是不正确的。CONDITION_EXPRESSION
是计算结果为布尔值的任何单个表达式。在您的情况下,您使用的是具有以下语义的昏迷运算符:A, B
将执行以下操作:- 评估 A(它将评估,而不仅仅是忽略)
- 放弃 A 的结果
- 评估 B
- 返回 B 作为结果
在您的情况下:
i<3,j<2
您正在比较i<3
,您只是忽略了此比较的结果。当指令有一些副作用时,逗号表达式很有用,而不仅仅是返回一个值。常见情况有:变量递增/递减或赋值运算符。
LOOP_EXPRESSION
是不需要计算任何值的任何单个表达式。这里再次使用逗号表达式,忽略左侧的结果。但是,在这种情况下,您无论如何都没有使用结果,而只是使用 ++ 副作用 - 即增加变量的值。BODY_BLOCK
是单个语句或块,用花括号封装。
The above for
can be compared to:
上面for
可以比作:
{
START_STATEMENT;
while(EXPRESSION) {
BODY_BLOCK;
LOOP_EXPRESSION;
}
}
回答by Dev
The c complier always used second condition.
c 编译器总是使用第二个条件。
therefore j<2 is used.
因此使用 j<2。
use this for loop
使用这个 for 循环
for(i=0,j=0;j<10;i++,j++)