+ 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.
8 Respostas
+ 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.
+ 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
+ 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
}
+ 2
Why do you think it does not make sense? What should it be?
0
now it makes some sense .... still need to code it myself to gain confidence !!
0
make ++x
0
for loops for starters, conditional statements where you need to evaluate first before the value is incremented
0
Try this :
x=0
x++
System.out.println(x)
System.out.println(x)