What is the difference between (++c) & (c++)? [duplicate] - java

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
postfix and prefix increment operator in a for loop [duplicate]
(2 answers)
Closed 7 years ago.
What is the difference between (++c) and (c++)?
Lets say
c = 4
I know that for (++c) you would increment increment 4 by 1 so 5, but for (c++)?

Both c++ and ++c increment the variable they are applied to. The result returned by c++ is the value of the variable before incrementing, whereas the result returned by ++c is the value of the variable after the increment is applied.
example:
public class IncrementTest{
public static void main(String[] args){
System.out.println("***Post increment test***");
int n = 10;
System.out.println(n); // output 10
System.out.println(n++); // output 10
System.out.println(n); // output 11
System.out.println("***Pre increment test***");
int m = 10;
System.out.println(m); // output 10
System.out.println(++m); // output 11
System.out.println(m); // output 11
}
}
For more info, read this: http://www.javawithus.com/tutorial/increment-and-decrement-operators
Or google post increment and pre increment in java.

Related

In java, don't understand that simple while loop(the sum of 1 to 6) [duplicate]

This question already has answers here:
Difference between pre-increment and post-increment in a loop?
(22 answers)
Closed 7 months ago.
public class Main {
public static void main(String[] args) {
int i=1,sum=0;
while(i<=6) {
sum+=i++;
}
System.out.println(sum);
}
}
this is java eclipse code, and it prints 21 normally
But i don't understand that "sum += i++; " code.
I understood the meaning of that code as, 1+1 2+1 3+1 4+1 5+1 6+1 -> 2+3+4+5+6 -> 20.
How is that code calculated in a while loop? And why not ++i?
the answer is 21 because we add each iteration of the loop in the sum.
so at each verification if a number is less than or equal to <=6 we add each in the variable sum. so the increment is after verification and not make the post incrementention. #JAVA

Where is the sum being stored? [duplicate]

This question already has answers here:
Understanding recursion [closed]
(20 answers)
In a recursive statment, how does java store the past values?
(3 answers)
Reversing a String with Recursion in Java
(17 answers)
How are the numbers stored, called, and added in this short recursion example?
(3 answers)
Closed 3 years ago.
I'm attempting to create a method that will calculate the sum of a an integer, as the integer is broken down in to single integers.
E.g. 2546 becomes 2, 5, 4, 6. Then I plus those all together.
2 + 5 + 4 + 6 = 17
The method will run recursively.
I'd made this program non-recursively, but in that one, I had a variable to store a sum of the calculation.
public static int calcSum(int n){
if (n>0)
return ((n%10) + calcSum(n/10));
else
return 0;
}
The program works, I just don't understand how the sum is stored.
The sum is not stored in any variable (unless the caller of the recursive method will store the result in some variable).
The recursive method returns the sum to its caller without storing it in a variable:
return ((n%10) + calcSum(n/10));
or returns 0 if the input is 0.
calcSum(2546) returns 6 + calcSum(254)
calcSum(254) returns 4 + calcSum(25)
calcSum(25) returns 5 + calcSum(2)
calcSum(2) returns 2 + calcSum(0)
calSum(0) returns 0
so when the recursion unwinds:
calcSum(2) returns 2 + 0 == 2
calcSum(25) returns 5 + 2 == 7
calcSum(254) returns 4 + 7 == 11
and finally
calcSum(2546) returns 6 + 11 == 17

I can not figure out why am i getting the result 2 [duplicate]

This question already has answers here:
How is that x=20;x= ++x + ++x + x++ ;final value of x in java is 65 [duplicate]
(8 answers)
Closed 5 years ago.
Maybe I'm missing out but I can't figure out why I am getting the result 2 in this code:
i = 1;
i = i-- - --i;
System.out.println(i);
In i = i-- - --i you have:
i--, a post-decrement, which retrieves the current value of i (1) and then decrements i to 0
-
--i, a pre-decrement, which decrements i again and retrieves the updated value, -1
So you end up with i = 1 - -1 which is 2.
Needless to say, this sort of thing shows up on (silly) Java tests and such, but should never appear in production code.

Post-Increment in Recursive Method? [duplicate]

This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Java: Prefix/postfix of increment/decrement operators
(11 answers)
Closed 6 years ago.
I'm trying to learn java oop and i find some problem understanding why the use of post-increment in Recursive Method cause error ? I don't understand .
Main Class :
public class Main {
public static void main(String[] args) {
A a = new A();
a.res(0);
}
}
Code work fine :
public class A {
public void res(int a){
if (a < 5)
res(a+1);
System.out.println(a);
}
}
Output : run:
5
4
3
2
1
0
BUILD SUCCESSFUL (total time: 0 seconds)
But in when i use the ++ operator i got StackOverflowError .
public class A {
public void res(int a){
if (a < 5)
res(a++);
System.out.println(a);
}
}
You are confusing a++ and ++a. As per java documentation - "The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value".
In your case the recursive method is always called with the argument 0
The post-increment operator increments the variable after you use it, so it will only increment the variable after the recursive function is returned.
Your code is doing this:
res(a++) //a = 0
//now a = 1
The recursion will never reach that next line, so the recursive function will always call res(0)
References: https://stackoverflow.com/a/2371162/7238307
Well... what do you expect, exactly? Let's re-write your program slightly. Your program (the one that crashes) can effectively be re-written like so, achieving the same effect (that is, crashing due to a StackOverflowError):
public class A {
public void res(int a){
if (a < 5) {
res(a);
++a;
}
System.out.println(a);
}
}
Your variable a will only increment after the recursive call to res has completed... which is never, because you're always invoking res with the same value.
This is how the recusrive calls are resolved with a+1.
a=0, res(0)
+ 0 < 5, res(1)
++ 1 < 5, res(2)
+++ 2 < 5, res(3)
++++ 3 < 5, res(4)
+++++ 4 < 5, res(5)
++++++ 5 = 5, print 5
+++++ print 4
++++ print 3
+++ print 2
++ print 1
+ print 0
Regarding the problem of your code with a++ is that the increment is performed after the the recursive call is performed. So you basically get:
a=0, res(0)
+ 0 < 5, res(0)
++ 0 < 5, res(0)
+++ ...

I don't know why my variables are getting these values [duplicate]

This question already has answers here:
Java: Prefix/postfix of increment/decrement operators
(11 answers)
Closed 9 years ago.
public int Gauss_Jordan(double[][] matrix, int numOfRows, int numOfCols) {
for (int col_j = 0; col_j<numOfCols; col_j++) {
row_i = nonzeros ++;
System.out.println(row_i+" and "+nonzeros);
}
//return matrix;
return 0;
}
up above in the method called "Gauss_Jordan", you can see a for loop where it iterates until a certain condition is met. (duh.. lol sorry).
so i set row_i = nonzeros++ but here's the thing, when I print out each iteration i get
0 and 1,
1 and 2,
2 and 3
. I would expect the output to be:
1 and 1,
2 and 2,
3 and 3.
How come this is not the case?
You'd need ++nonzeros instead of nonzeros++ to get what you expect.
Thats called post-increment;
When you say row_i = nonzeros ++;
first the row_i will get assigned with the value of nonzeros and the nonzero will get incremented.
try pre-increment
row_i = ++nonzeros;
If pre-increment is not what you wanted. Check the initialization of nonzeros and change it into '1` for it to show up as you want. Your codes are functioning as they should.

Categories