Is this code even correct? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this code even correct?

I don't understand what this code means which is often used in the tutorials from sololearn, but nowhere else: int x = 1 int y = x++ //now in y stands 2 std:cout << ++y << endl what us this ++ thing in cout? it makes no sense and I don't think it's good programming behaviors especially for beginners.

12th Jan 2017, 9:28 PM
Abraham Soeyler
Abraham Soeyler - avatar
3 Answers
+ 8
Line 'y=x++;' - y gets the value of x, so y is now 1, and then x++ increments the value of x, so now x=2 Line 'cout<<++y<<endl;' y gets incremented so y is now 2, and then it is printed
12th Jan 2017, 10:05 PM
Filip
Filip - avatar
+ 4
go back and do lesson 1:11 Basic Concepts. Assignment and Increment EDIT: They explain it very well.
12th Jan 2017, 10:25 PM
jay
jay - avatar
+ 1
ah yeah, so i got that correct. dont get me wrong,i work with C since 7 years, but i have never seen this kind of line with cout ... i was just wondering why they use it that way
12th Jan 2017, 11:21 PM
Abraham Soeyler
Abraham Soeyler - avatar