0

how output 26? #include <iostream> using namespace std; int main() { int x=1; for(;x<5;x++) { x+=x; cout<<x; } }

23rd Aug 2016, 5:12 AM
Lekhraj Singh
Lekhraj Singh - avatar
2 Réponses
+ 5
Try the code below. If you don't understand again study again the for loop. #include <iostream> using namespace std; int main() { int x=1; for(;x<5;x++) { x+=x; cout<<x<<endl; }
23rd Aug 2016, 5:23 AM
GTimo
GTimo - avatar
+ 5
I think, the question is WHY, and not HOW. just two hints. First, it's not the best to manipulate the variable of for loop within the loop, it may seriously alter the behavior of the loop. That's what exactly happened here. Second, put '<< endl' to the end of the cout line. The output will 2 6 So you'll have a much clearer viewpoint why your code works this way.
23rd Aug 2016, 7:17 AM
Tamas Szekffy
Tamas Szekffy - avatar