Incrementing and decrementing variables (prefix vs suffix) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Incrementing and decrementing variables (prefix vs suffix)

x++ uses x’s value in the next place it’s used THEN increments it right? For example: x=2; System.out.println(x++); System.out.println(++x); //Outputs 2 and 4 But in this example: int x = 1; do { System.out.println(x); x++; } while(x < 3); I do not understand why it doesn’t print 1, 2 and 3. (It prints 1 and 2). Here is how I think: int x = 1; do { System.out.println(x); //print 1 x++; } while(x < 3); // First time: is x (1) < 3? Yes so it does it again. After it has checked, x = 2.

26th Dec 2017, 12:36 PM
EricMcDerrick
EricMcDerrick - avatar
2 Answers
+ 1
I was just wondering why it increments before testing.
26th Dec 2017, 4:06 PM
EricMcDerrick
EricMcDerrick - avatar
0
@Martin Taylor. Idk if the tagging works, but I’ll try :P
26th Dec 2017, 8:36 PM
EricMcDerrick
EricMcDerrick - avatar