Why my code print 0000000000 ?(sorry my bad english) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

20th Feb 2020, 2:53 AM
Tài Nguyễn.G
Tài Nguyễn.G - avatar
5 Answers
+ 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; }
20th Feb 2020, 3:24 AM
DishaAhuja
DishaAhuja - avatar
20th Feb 2020, 5:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
20th Feb 2020, 3:02 AM
DishaAhuja
DishaAhuja - avatar
20th Feb 2020, 6:15 AM
Ömer Ayhan
Ömer Ayhan - avatar
+ 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
21st Feb 2020, 5:07 PM
Syed Wasif Shah
Syed Wasif Shah - avatar