Learn to Code via Tutorials on Repl.it!

← Back to all posts
n++; and ++n;!
firefish (1004)

What's the difference?

You've probably used them to increment variable n, but that's not just what they're used for.


Let's say n = 3;. Let's now do this: Console.WriteLine(n++);. Yes, that's right, n++ and ++n are expressions! n++ will return n, then increment n. So Console.WriteLine(n++); will print 3!


n = 3; again. ++n will increment n, then return n.
So Console.WriteLine(++n); will print 4!


+= is like ++n;. So if n = 3; again, Console.WriteLine(n+=10); will increment n by ten, then print n! So this will print 13!

This is in C#, C++ and C

So @HahaYes will find this works as well.

firefish (1004)

Also forogt to mention @HahaYes @DynamicSquid @CodeLongAndPros will all find this will work