+ 2
Why my code print 0000000000 ?(sorry my bad english)
j++ <=> i add 1, right ? So i try, j=j++ in loop For (int i=0; i<10; i++){ j=j++; cout<<j; } And it print 0000000000 not 123456789 ? My code https://code.sololearn.com/cT7o9x3KDyDE/?ref=app
5 Respostas
+ 3
Tài Nguyễn.G yes because in the sequence assignment the first value which is readed is j=0 and the post increment will assign the 0 value to j so this way every time printed value of j will be 0 so 10 times 0 will be printed.
You have to do like this.
#include <iostream>
using namespace std;
int main() {
int j=0;
for (int i=0; i<10; i++){
j++;
cout<<j<<endl;
}
return 0;
}
+ 3
Take a look at
https://www.sololearn.com/Discuss/1271878/?ref=app
+ 2
Tài Nguyễn.G
It is not specified when between sequence points modifications to the values of objects take effect in your assignment of J = j++ thats why the sequence error is giving warning ⚠.
So just replace that line with only
j++;
And rest of the code will work fine.
+ 2
Here's the fixed version :
https://code.sololearn.com/c36kmm7AUNsy/?ref=app
+ 1
This is the mistake :
j=j++;
It should be :
j=++j;
As you are using j++ which means post increment . j=j++; here means that first assignment will take place i,e j=0; then increment j's value . So when assignment will take place first then how could be j can incremented
So try to use j=++j;
Now j's value will be first incremented then the assignment will take place
Like from 0 to 1 then 1 to 2 .... thanks