在'for'循环中使用相等 - 为什么不呢? [英] Using equality in 'for' loop - why not?

查看:85
本文介绍了在'for'循环中使用相等 - 为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

''for''循环需要3个参数(initialize; test; increment)。 ''测试''

必须等同于真或假


这不起作用......

x = 5;

for(y = 1;(y == 5); y + = 1){

alert(x * y);

}

...也不...

x = 5;

for(y = 1;(y === 5) ; y + = 1){

alert(x * y);

}

....但是这样做..

x = 5;

for(y = 1;(y <6); y + = 1){

alert(x * y);

}


为什么前两个失败?如果我是值5然后i == 5是真的,那么

i === 5.


任何人都可以解释我在这里缺少的东西?


问候


解决方案

文章< c9 ** *****************@news.demon.co.uk> ;,马克安德森

说...

A ''for''循环需要3个参数(initialize; test; increment)。 ''测试''
必须等同于真或假

这不起作用......
x = 5;
for(y = 1 ;(y == 5); y + = 1){
alert(x * y);
}


这是失败的,因为在第一个循环之后 Y'QUOT;成为12.尝试

for(y = 1; y!= 5; y ++)


为什么前两个失败?如果i是值5,则i == 5为真,
i === 5。


循环在条件为真时执行。在你的例子中你

为y分配1,所以条件y == 5是假的。


有谁可以解释我在这里缺少的东西?




你错过了文档:
http://tinyurl.com/jbie


-

Hywel我不吃乳蛋饼
< a rel =nofollowhref =http://kibo.org.uk/target =_ blank> http://kibo.org.uk/
http://kibo.org.uk/mfaq.php

<马克安德森写道:


马克

'''for''循环需要3个参数(初始化;测试; 增量)。 ''测试''
必须等同于真或假

这不起作用......
x = 5;
for(y = 1 ;(y == 5); y + = 1){
alert(x * y);
}


是的,我认为它有效。

只是没有预期的那样。

你的''测试''第一次失败,因为y = 1而不是5.


想想for循环如下,可能有帮助:


for(initialize; test; increment){

语句;

}


等于:


初始化;

while(test){

语句;

增量;

}

现在你也可以解释其余的。 :-)

..nor确实...
x = 5;
for(y = 1;(y === 5); y + = 1){
alert(x * y);
}
...但这确实..
x = 5;
for(y = 1;(y <6) ; y + = 1){
警报(x * y);
}

为什么前两个失败?如果我是值5然后i == 5是真的,那么
我=== 5.

任何人都可以解释我在这里缺少的东西吗?



问候,

Erwin Moller


Mark Anderson说:


''for''循环需要3个参数(initialize; test; increment)。 ''测试''
必须等同于真或假

这不起作用......
x = 5;
for(y = 1 ;(y == 5); y + = 1){
警报(x * y);
}
..没有......
x = 5;
for(y = 1;(y === 5); y + = 1){
alert(x * y);
}
...但这确实.. < br => x = 5;
for(y = 1;(y <6); y + = 1){
alert(x * y);
}

为什么前两个失败?如果我是值5然后我= = 5是真的,那么我= = 5.

任何人都可以解释我在这里缺少的东西吗?




你设置x = 5和y = 1并且似乎对y!= 5感到惊讶。

不知道你,这很难猜测这是否是疏忽

或者你是否真的遗失了什么。


A ''for'' loop takes 3 arguments (initialize; test; increment). The ''test''
must equate as true or false

This doesn''t work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
...nor does...
x = 5;
for (y=1; (y===5); y+=1) {
alert(x * y);
}
....but this does..
x = 5;
for (y=1; (y<6); y+=1) {
alert(x * y);
}

Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.

Can anyone explain what I''m missing here?

Regards


解决方案

In article <c9*******************@news.demon.co.uk>, Mark Anderson
says...

A ''for'' loop takes 3 arguments (initialize; test; increment). The ''test''
must equate as true or false

This doesn''t work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
This fails because after the first loop "y" becomes 12. Try
for (y=1; y!=5; y++)

Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.
The loop executes while the condition is true. In your example you
assign 1 to y, so the condition y==5 is false.

Can anyone explain what I''m missing here?



You''re missing the documentation:
http://tinyurl.com/jbie

--
Hywel I do not eat quiche
http://kibo.org.uk/
http://kibo.org.uk/mfaq.php


Mark Anderson wrote:

Hi Mark

A ''for'' loop takes 3 arguments (initialize; test; increment). The ''test''
must equate as true or false

This doesn''t work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
yes, I think it works.
Just not as expected.
Your ''test'' fails the first time because y=1 and not 5.

Think of for-loop as follows, maybe that helps:

for (initialize; test; increment){
statements;
}

is equal to:

initialize;
while(test) {
statements;
increment;
}
Now you can explain the rest too. :-)
..nor does...
x = 5;
for (y=1; (y===5); y+=1) {
alert(x * y);
}
...but this does..
x = 5;
for (y=1; (y<6); y+=1) {
alert(x * y);
}

Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.

Can anyone explain what I''m missing here?

Regards


Regards,
Erwin Moller


Mark Anderson said:


A ''for'' loop takes 3 arguments (initialize; test; increment). The ''test''
must equate as true or false

This doesn''t work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
..nor does...
x = 5;
for (y=1; (y===5); y+=1) {
alert(x * y);
}
...but this does..
x = 5;
for (y=1; (y<6); y+=1) {
alert(x * y);
}

Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.

Can anyone explain what I''m missing here?



You''re setting x=5 and y=1 and seem to be surprised that y!=5.
Without knowing you, it''s hard to guess whether this is oversight
or if you really are missing something.


这篇关于在'for'循环中使用相等 - 为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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