while循环需要显式条件,for循环不,为什么?

编程入门 行业动态 更新时间:2024-10-09 13:21:00
本文介绍了while循环需要显式条件,for循环不,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C ++中,允许在for循环中有一个空条件,例如 for(;;)或 for int x = 0 ;; ++ x)。但是你不能做 while()。

In C++ you are allowed to have an empty condition inside the for-loop, for example as in for (;;) or for (int x = 0;; ++x). But you can't do while ().

当在for循环中省略条件时,要 true (所以循环永远循环)。为什么而循环不是这种情况,也就是说,不允许 while() 的别名

When condition is omitted inside the for loop, condition is assumed to be true (so the loop loops forever). Why isn't this the case with while loops, that is, what's the argument behind not letting while () be an alias of while (true)?

推荐答案

在for语句中每个给定的子句是可选的事实的副作用。有一些原因为什么一些for循环不需要一个赋值;有理由为什么有些人不需要条件;有其他人不需要增量的原因。要求有一定的最小数量将是不必要的增加的复杂性。

Presumably, it's a side-effect of the fact that each given clause within the for-statement is optional. There's reasons why some for-loops wouldn't need an assignment; there's reasons why some others wouldn't need a condition; there's reasons why still others wouldn't need an increment. Requiring there to be some minimum number of them would be needlessly-added complexity.

更多推荐

while循环需要显式条件,for循环不,为什么?

本文发布于:2023-11-23 13:15:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!