您好,欢迎来到花图问答。
搜索
您的当前位置:首页for循环和while循环的区别

for循环和while循环的区别

来源:花图问答

之前学习C++的时候都觉得for循环和while循环没有什么区别的,今天看到C++primer中的介绍才知道这两个循环在编程使用上还是有一点的不同的。
  这是书中的原文:

Typically,programmers use for loops for counting loops because the for loop format enables you to place all the relevant information—initial value,terminating value,and method of updating the counter—in one place.
Programmers most often use while loops when they don’t know in advance precisely
how many times a loop will execute.

当时就觉得很受启发,虽然这是编程感悟上的一些事情了,但是自己有了自己的感受还是觉得很满足的。


2.for循环的新特性

在最新的c++11标准中,for循环有了新的形式

int number[10] = {1,2,3,4,5,6};
for(int & x:number)//只要写入数组名就能够进行循环
    cout<<x<<endl;//如果不需要修改数组中的值得话,x可以不是引用

这种for循环主要是对于遍历比较方便。

Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务