简体   繁体   English

javascript循环似乎不起作用

[英]javascript loop does not seem to work

I expected the code snippet to print out 100 times with the value of i. 我希望该代码段以i的值打印100次。

在此处输入图片说明

But it prints out only once like below. 但是它只打印一次,如下所示。

What am I missing here ? 我在这里想念什么?

 let i = 0; for (; i++; i < 100) { console.log('Loop ==>' + i); } console.log('Loop Done'); console.log('Value of i ==>' + i); 

You have to write loop like below - 您必须编写如下所示的循环-

 <script>
    let i=0;
                        for(;i<100;i++)
                        {
                              console.log('Loop ==>'+i);
                        }
                        console.log('Loop Done');
                        console.log('Value of i ==>'+i);        
    </script> 

You mixed the "condition" and "change" places in your for loop. 您在for循环中混合了“条件”和“更改”位置。 Condition is in the second place and change in the third place. 条件排在第二位,变化在第三位。

 let i = 0; for(; i < 100; i++) { console.log('Loop ==>'+i); } console.log('Loop Done'); console.log('Value of i ==>'+i); 

Your for loop's structure is faulty. 您的for循环结构错误。 You should use the correct syntax, like this. 您应该使用正确的语法,如下所示。

for (; i < 100; i++)

for loops have 3 statements, initialization, condition and updation, in that order. for循环按该顺序包含3条语句,分别是初始化,条件和更新。 The loop runs as long as the second statement, ie, the condition is met. 只要第二条语句(即条件)得到满足,循环就会运行。 The third statement, ie, updation, is executed after all the code in your block is executed. 第三条语句(即更新)在执行块中的所有代码之后执行。

What you have done is you have mixed up the third (updation) and the second (condition) statements in your for loop. 您要做的是在for循环中混合了第三条(更新)和第二条(条件)语句。

Change ; i++; i < 100 变化; i++; i < 100 ; i++; i < 100 ; i++; i < 100 to ; i < 100; i++ ; i++; i < 100; i < 100; i++ ; i < 100; i++ ; i < 100; i++ . ; i < 100; i++ In the former case i value is incremeneted and tested if less than 100, whereas it need the opposite 在前一种情况下,如果值小于100,则我的价值会增加,并经过测试,而相反

 let i = 0; for (; i < 100; i++) { console.log('Loop ==>' + i); } console.log('Loop Done'); console.log('Value of i ==>' + i); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM