BinaryArt 10 發表於 October 26, 2008 檢舉 Share 發表於 October 26, 2008 程式碼如下:#include <iostream>using namespace std; int main(){ int loop = 0; cout << "Please input the times you want to repeat: "; cin >> loop; for(int i=1; i<=loop; i++) { cout << i << endl; } cout << "Loop is ending!" << endl; system("PAUSE"); //讓輸出的DOS畫面能夠停駐 return 0;}--------------------------------------------------------------------------------------------------------------------------問題:在 for(int i=1; i<=loop; i++) 裡 i++ 改成 ++i 為何不影響結果? 沒有前置跟後置的差別嗎?還有,若改成 for(int i=1; i++; i<=loop ) 他將會無限循環 又是為什麼? 跟原來的差別在哪?例如我輸入重複次數為五 那麼在執行第六次時 i值為六 應該會停止才對阿? 鏈接文章 分享到其他網站
TerryW 10 發表於 October 27, 2008 檢舉 Share 發表於 October 27, 2008 i++和++i都是遞增,如果只單純做這個就沒什麼前置後置的差別了至於for(int i=1; i++; i<=loop )....你還是再把for迴圈的部分看仔細吧@@ 鏈接文章 分享到其他網站
江湖舊夢 10 發表於 October 27, 2008 檢舉 Share 發表於 October 27, 2008 函式每個位置都有其所具有的特定意義,不可掉換在for中是:變數;迴圈執行條件;迴圈到底時所執行的動作故for(int i=1; i++; i<=loop )中的i++,編譯器將非0的值皆視為ture,故會造成無窮迴圈i++的意思是先執行i,再執行i=i+1,++1則是先執行i=i+1,再執行i在你的程式中,i++與++i並不會造成結果的差異,因為不論是哪種最後的結果都是遞增1 鏈接文章 分享到其他網站
Recommended Posts
請登入後來留意見
在登入之後,您才能留意見
立即登入