java for 执行顺序,Java for循环的确切执行顺序是什么?

博客探讨了JavaScript中两种不同形式的for循环执行顺序的理解误区。第一种是标准的for循环,第二种是使用后置递增运算符的循环。文章指出,尽管第二种方式更简洁,但两者的执行效果相同。作者解释了为何在第二次检查条件时,循环不会立即退出,并澄清了for循环初始化、条件检查和迭代更新的顺序。文章强调,不使用第三个表达式并不节省时间,而是一种语法糖,主要为代码风格,并非性能优化。
摘要由CSDN通过智能技术生成

I'm a bit stumped by the exact order of execution of a particular for loop I'm playing with, in Codepen. Check out these two loops, which both do the same thing:

var a = ['orange','apple'];

for (var i=0; i<2;i++) {

alert(a[i]);

}

for (var j=0, fruit; fruit = a[j++];) {

alert(fruit);

}

The first loop is the standard, vanilla way of writing a for loop. As expected, it alerts "orange", then "apple".

It works, but I don't quite understand why. My understanding of for loops, which is obviously incorrect in some way (more than one way?) is that:

* The for loop evaluates the truthiness of the second expression before it runs the first time. So it seems that when the for loop is evaluating the truthiness of the second expression, it should increment j before setting the value of fruit. So the first time through the loop, fruit should equal "apple".

* And the loop should only run once, because after the first time through, when it's evaluating the second expression again to see if it's still true, it should once again increment j before it returns a value to fruit, and since there is no value at index position 2, it should return a falsy result, and exit the loop.

I don't understand what actually IS happening. Does the for loop run once before the second expression is evaluated? But that seems impossible, because if that happened, the alert would alert nothing the first time through, since fruit wouldn't yet have a value.

Also, the author of the MDN article seemed to like the second way of writing the loop - is there a reason that way is better? It uses fewer characters, which is good, I guess. Does skipping the third expression in the for loop save significant time? Or is it just a "it's cool because it's clever" sort of thing?

解决方案

for (var j=0, fruit; fruit = a[j++];) {

alert(fruit);

}

In pseudocode is equal to:

initialize j = 0 and fruit = undefined

assign fruit = a[j] (j = 0, fruit = 'orange')

j = j + 1 (j = 1)

check fruit for being truthy (true)

alert(fruit) ('orange')

assign fruit = a[j] (j = 1, fruit = 'apple')

j = j + 1 (j = 2)

check fruit for being truthy (true)

alert(fruit) ('apple')

assign fruit = a[j] (j = 2, fruit = undefined)

j = j + 1 (j = 3)

check fruit for being truthy (false)

exit loop

Important note:

The postfix unary ++ operator works as:

We return the current value of the variable

We increment the value of the variable

Does skipping the third expression in the for loop save significant time?

It does not save anything at all

Also, the author of the MDN article seemed to like the second way of writing the loop - is there a reason that way is better?

It's not better in any way. Author just thinks it's cool and author likes to be cool (while they are not).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值