【发布时间】:2020-11-06 10:52:43
【问题描述】:
也许我的问题看起来很重要,但实际上并非如此。我声称我可以做到一切是由for() while() 完成的,反之亦然。那么当有for() 时while() 真的有用吗?我需要一个例子,我可以通过for() 做到这一点,而我不能通过while() 做到这一点。有?我认为没有……!
结构如下:
for (init counter; test counter; increment counter) {
code to be executed;
}
init counter;
while (test counter) {
code to be executed;
increment counter;
}
看到了吗?它们完全一样,现在我想知道为什么 php 两者都有?
【问题讨论】:
-
是的!!!我认为有一个区别。如果您知道迭代次数,请执行 For 循环,否则执行 While 循环。还有一件事是 For 循环更具可读性和更精确的方式
标签: php for-loop while-loop