三種不一樣的循環結構以及嵌套是必須認識與可以使用的ios
三種循環結構分別是:c++
在for循環中能夠在for後的括號內加入初始條件,循環條件與參數變化。使得整個循環體看起來簡潔明瞭。spa
其表達式爲for(表達式1(能夠被省略);表達式2;表達式3(能夠被省略)),例如:code
include <iostream> using namespace int main() { int a; for(a=1;a<=10;a++) { cout<<"a="<<a<<endl; } return 0; }
do while 與 while 都須要在循環體內加入控制參數的變化,其中 do while會在先執行以前判斷執行條件,而while是先執行再判斷,是否繼續執行。blog
其使用格式爲:do while(終止條件),while(終止條件)。io
例如:for循環
#include <iostream> using namespace int main() { int a=1, do while(a<1) { a++; } cout<<"a="<<a<<endl; a=1; while(a<1) { a++; } cout<<"a="<<a<<endl; return 0; }
三者的嵌套遵循優先執行最裏層的循環,同在循環中定義的變量沒法在外層使用。class
#include <iostream> using namespace int main() { int a,b,c,sum; for(a=1,b=1,c=1;a<10;a++) { do while(b<10) { while(c<10) { sum=a*b*c; cout<<"sum="<<sum<<endl; } } } }
重複使用代碼不只能夠免去不少麻煩,還能夠使得軟件的功能能夠順利進展stream