Why ++x and x++ are showing same result in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why ++x and x++ are showing same result in this code?

public class Program { public static void main(String[] args) { for(int x = 1; x <=5; x++) { System.out.println(x); } } }

26th May 2020, 4:08 PM
Harsh Vyas
Harsh Vyas - avatar
1 Answer
+ 7
You'll get the answer if you try it this: System.out.println(++x) and System.out.println(x++) 😉 Okay, little explanation: You increased the values independent from the print-command. So it is the same either you increase it post or pre loop header.
26th May 2020, 4:18 PM
Sandra Meyer
Sandra Meyer - avatar