0
how output 26? #include <iostream> using namespace std; int main() { int x=1; for(;x<5;x++) { x+=x; cout<<x; } }
2 Answers
+ 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;
}
+ 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.