for(int i=0;i<10;i++){
cout<<i;
}
分析程序運行結果:
for(cout<<"a";cout<<"b";cout<<"c"){
cout<<"d";
}
自己回去練習,寫出輸出結果是什么?
#include <iostream> using namespace std; int main(){ int x=1; for(cout<<"a";x<3;cout<<"c"){ cout<<"d"; x++; } }
最先先輸出賦初值語句,僅僅只輸出1遍 也就是int i=0這句最先執行,且僅一次 //輸出 a
然后執行判斷語句 ,就是執行x<3這句,如果條件滿足,立即執行循環體,也就是//輸出 d
cout<<"d";
接下來執行自增//輸出 c
然后輸出 d
然后輸出 C
for(;;){
cout<<"Say something";
}
舉例:
#include <iostream> using namespace std; int main(){ int flag=1; for(;;){//當沒有的時候,這個語言默認是為真還是為假 if(flag==10){ cout<<"你已經殺死了"<<flag<<"只怪物,可以過關了"<<endl;//以后只要需要無線循環的時候,都可以這樣用 break; } flag++; cout<<"你殺死了 "<<flag<<" 只怪物"<<endl; } return 0; }