+ 2

If x=5 and x++ still =5 then what's the point?

I'm sure there is a reason or a practical use but it doesn't make sense to me.

14th Jul 2016, 6:22 PM
Mike Hyman
Mike Hyman - avatar
8 Answers
+ 3
Wherever var++ (postfix operator) is used, it places the current value of variable "var" in that position and then increases its value for its next use. for example, see this code: int x=5; System.out.println(x); System.out.println(x++ ); System.out.println(x); System.out.println(x++ + x++); Output: 5 5 6 13 Analyse the code and output. These operators are extremely helpful and have a huge practical use when it comes to make shorter and meaningful programs.
17th Jul 2016, 12:54 PM
Vibhu Agarwal
Vibhu Agarwal - avatar
+ 2
the practical use x++ is in for loop, where after checking the condition value is incremented....if it is ++x then loop will run n-1 times instead of n times
14th Jul 2016, 6:56 PM
deepanshu7211
+ 2
There are two types of increments post increment and pre increment. Post Increment: out put the value and then add new value. Pre increment: add new value and then print the value. Execution in for: this will run 10 times. for( int x = 0; x < 10; x++ ){ // code to execute } this will run 9 times. for( int x = 0; x < 10; ++x ){ // code to execute }
14th Jul 2016, 8:03 PM
Ali Shan
Ali Shan - avatar
+ 2
Why do you think it does not make sense? What should it be?
16th Jul 2016, 1:24 PM
WPimpong
WPimpong - avatar
0
now it makes some sense .... still need to code it myself to gain confidence !!
17th Jul 2016, 12:44 PM
Chitrang Shukla
Chitrang Shukla - avatar
0
make ++x
19th Jul 2016, 7:24 PM
Dennis Sam
Dennis Sam - avatar
0
for loops for starters, conditional statements where you need to evaluate first before the value is incremented
22nd Jul 2016, 4:46 AM
Eric Gitangu
Eric Gitangu - avatar
0
Try this : x=0 x++ System.out.println(x) System.out.println(x)
26th Jul 2016, 6:14 AM
Ivan
Ivan - avatar