C++

Can anyone explain this to me?
#include <iostream>
#include <cmath>
using namespace std;

int main(){
int i = 0;
while(i<10){
i=i*2;
cout << "Value of i is: " << i <<endl;
i++;
}
cout << "Final value of i is: " << i <<endl;

return 0;
}
Have you tried running it yet?

Click the little gear, and then click "Run" on the next page. That will let see what the code does.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int i = 0;
    while(i<10){
        i=i*2;
        cout << "Value of i is: " << i <<endl;
        i++;
    }
    cout << "Final value of i is: " << i <<endl;

    return 0;
}
Eu vi o que o código faz, mas quero explicação detalhada
It might help you understand what's happening if you also print the value of i at the top of the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int i = 0;
    while(i<10){
	cout << "At top of loop i = " << i << endl;
        i=i*2;
        cout << "Value of i is: " << i <<endl;
        i++;
    }
    cout << "Final value of i is: " << i <<endl;

    return 0;
}

At top of loop i = 0
Value of i is: 0
At top of loop i = 1
Value of i is: 2
At top of loop i = 3
Value of i is: 6
At top of loop i = 7
Value of i is: 14
Final value of i is: 15


Please study this version and the output. If you still don't understand, ask specific questions.
Topic archived. No new replies allowed.