My interest in learning programming was recently rekindled, and so I picked up a copy of C++ Without Fear, recommended on the beginner list at this site. I've ran into a question for which I can't figure out the answer to on my own. The example code is:
Code:
#include <iostream>
using namespace std;
int main() {
int i, n;
cout << "Input a number and press ENTER: ";
cin >> n;
i = 1;
while (i <= n) {
cout << i << " ";
i = i + 1;
}
return 0;
}
....But whoops, I made an error in the first line of the while loop:
Code:
...
while (i <= 1)
...
When running this flawed code, it seems to me that the while loop is stuck on the line , as a continuous stream of 1's is printed. If any of you can explain to me exactly what is happening, that would be great. In my beginner mind, this flawed while loop should run only once. What am I missing?