之前學習C++的時候都覺得for循環(huán)和while循環(huán)沒有什么區(qū)別的伞租,今天看到C++primer中的介紹才知道這兩個循環(huán)在編程使用上還是有一點的不同的氯庆。
這是書中的原文:
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.
當時就覺得很受啟發(fā)命咐,雖然這是編程感悟上的一些事情了景殷,但是自己有了自己的感受還是覺得很滿足的隙赁。
2.for循環(huán)的新特性
在最新的c++11標準中仇祭,for循環(huán)有了新的形式
int number[10] = {1,2,3,4,5,6};
for(int & x:number)//只要寫入數(shù)組名就能夠進行循環(huán)
cout<<x<<endl;//如果不需要修改數(shù)組中的值得話披蕉,x可以不是引用
這種for循環(huán)主要是對于遍歷比較方便。