2011-03-26 54 views
0

我的問題很簡單。我在C++程序中有一個'for'語句,當我編譯時忽略了我的cout。非常簡單的C++語句,但它不會cout?

我使用的Xcode,在Xcode編譯和這裏是我的代碼:

#include <iostream> 
using namespace std; 

    int main() 
    { 
     cout << this prints" << endl; 
     for(int i=0; i>10; i++) 
     { 
     cout << "this doesn't" << endl; 
     } 
    return 0; 
    } 

問題是什麼?

回答

10
for(int i=0; i>10; i++) 

初始化i0那麼只有進入循環體,如果i10更大。

環路只要環路爲條件i > 10是真的,不直到條件i > 10是真實的。這就是C++中所有循環的工作原理:for,whiledo/while

4

您的循環條件反向。你想要它是i < 10

3

您已得到循環不正確的條件。這應該工作。以下一經查看:

#include <iostream> 
using namespace std; 

int main() 
{ 
    cout << "this prints" << endl; 
    for(int i=0; i<= 10; i++) // ------> Check the change in condition here 
    { 
     cout << "this doesn't" << endl; 
    } 
    return 0; 
}