11-10-2009, 11:45 PM
Code:
#include <iostream>
using namespace std;int main(){int x=1;while(x<=99){x=x+1;cout<<x<< endl;return 0;}}
The output is 2, why isn't it:
2
3
4
5
6
7
8
9
10
etc...
Why doesn't this loop work?
|
11-10-2009, 11:45 PM
Code: #include <iostream> The output is 2, why isn't it: 2 3 4 5 6 7 8 9 10 etc...
Because you return the function within the while loop....
Your code Code: return 0;}} Code: }return 0;}
11-11-2009, 02:19 AM
Using return will stop the program in it's track, no matter what operation it's currently undergoing. For example, you can use return 1 for error checking.
11-11-2009, 07:14 AM
(11-10-2009, 11:45 PM)nevets04 Wrote: Please tell me you don't really lay your code out like that? Code: #include <iostream> Code: #include <iostream> The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
11-11-2009, 12:04 PM
(11-11-2009, 07:14 AM)MrD. Wrote: Please tell me you don't really lay your code out like that? I do that for shorter programs |
« Next Oldest | Next Newest »
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
For loop problem | GeneralOJB | 9 | 1,798 |
12-17-2009, 03:47 PM Last Post: GeneralOJB |