gpt4 book ai didi

javascript - While 和 For 循环

转载 作者:行者123 更新时间:2023-12-03 11:14:01 24 4
gpt4 key购买 nike

<分区>

我不明白为什么这个函数可以作为一个 While 循环,而不是一个 For 循环。我试图数到 10 并在每次迭代时打印到控制台。

While 循环:

function oneToTenW() {
var x = 0;
while(x < 10) {
x++;
console.log ("x is equal to " + x);
}
}

For 循环:

function oneToTenF() {
for(var x = 0; x < 10; x++);
console.log ("x is equal to " + x);
}

当我调用 While 循环时,我得到了这个:

oneToTen();
x is equal to 1
x is equal to 2
x is equal to 3
x is equal to 4
x is equal to 5
x is equal to 6
x is equal to 7
x is equal to 8
x is equal to 9
x is equal to 10

而当我调用 For-Loop 时,我得到了这个:

oneToTenf();
x is equal to 10

有什么想法吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com