Why i += i + a[i++] + a[i++] + a[i++] results in 8? - java

I am totally lost why I get these results:
int i = 1;
int[] a = new int[6];
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
a[4] = 4;
a[5] = 5;
i += i + a[i++] + a[i++];
//i is 5
i = 1;
i += i + a[i++] + a[i++] + a[i++];
// i is 8
I (wrongly) thought that there are these options:
i = i(=1) + a[i++] + etc - meaning that i = 1 is cached and not
changed then. Expression evaluation order is exactly from left to
right, I guess (?!).
i is increased, which results (for first example) in i = i(=3) + a[1] + a[2] now i = 3 and written to leftmost i
value from rightmost a[THIS_VALUE] is substituted into leftmost i, and last increment is never made.
But actual results leave me with no clue...

i += i + a[i++] + a[i++];
adds the original value of i to the value of the expression i + a[i++] + a[i++] and assigns the result to i.
It's equivalent to
i = i + i + a[i++] + a[i++];
1 + 1 + a[1] + a[2] = 1 + 1 + 1 + 2 = 5
Then you assign 1 to i and calculate:
i += i + a[i++] + a[i++] + a[i++];
which is equivalent to
i = i + i + a[i++] + a[i++] + a[i++];
1 + 1 + a[1] + a[2] + a[3] = 1 + 1 + 1 + 2 + 3 = 8
The important thing to note is that each a[i++] increments i, but accesses the element of a at the index of the previous value of i (i.e. the value prior to the increment).
Therefore the first a[i++] returns a[1] (even though i is incremented to 2), the second a[i++] returns a[2] (even though i is incremented to 3) and the third a[i++] returns a[3] (even though i is incremented to 4).

This important to notice differences between i++ and ++i, when you use i++ it use i value to calculate the operation and then increment i, but when you are using ++i it increment i and the calculate the operation.
it also worth mentioning that i+=X is equal to i = i + X.
So
i += i + a[i++] + a[i++];
is equal to
i = i + i + a[i++] + a[i++]
for i = 1 it is
i = 1 + 1 + a[1] + a[2]
which with your initialisation it will be :
i = 1 + 1 + 1 + 2
and thats how you get i = 5. and same for next line.

Its quite easy acutally, your using post increment. Post increment increments the value after the expression was executed, versus pre which increments before the expression is executed. So i += i + a[i++] + a[i++] + a[i++];, is 1 += 1 + 1 + 2 + 3, the first access, uses 1, then increment, next access uses two then increment then last access uses three.

Related

unexpected result while evaluating the operators

I have below code and it gives an unexpected result. To my understanding, the result should be 6 but its 1. Can someone help me how to get it?
int j = 0;
int i1 = j*5+ ++j;
System.out.println("j =" + j);
System.out.println("i1 =" + i1);
The difference between getting 6 and getting 1 is whether you think j*5 will be evaluated first or ++j will be evaluated first. The rule in Java is that subexpressions are evaluated in the order they appear in the expression if the order is not forced by dependencies. Here, j*5 appears first in the expression, so it is evaluated first. That gives the 1 result.
You have something like the code below:
int j = 0;
int i1 = j*5+ ++j;
System.out.println("j =" + j);
System.out.println("i1 =" + i1);
You need to edit your code to this:
int j = 0;
int i1 = ++j*5 + j;
System.out.println("j =" + j);
System.out.println("i1 =" + i1);
Your final output for "i1 =" + i1) will be 6.
The issue is that in your first code sample you are not incrementing J before you multiply it by 5 so you're only increasing it by +1 because you don't actually increment j until you've multiplied 0 * 5. (0 * 5) + 1 = 1. What I did was simply increment j (in prefix form) so that it increments by 1, and now you're multiplying (1 * 5) + 1.

What is the order of multiple pre/post incrementations in one line equation in JAVA?

I have this piece of code in JAVA 8:
int x=3;
int y = --x + x++ + --x ;
System.out.print("y: " + y + " x: "+x);
As I understand it should be split like this:
y = 2 + x++ + --x; x = 2
y = 2 + 2 + --x; x = 2
y = 2 + 2 + 1; x = 1
y = 5; x = 2
And it prints y: 6 x: 2
What is the order of operation in here?
It's because of the way pre and post increments work, it gets evaluated like this:
1. y = --x + x++ + --x ;
2. y = 2 + (2)++ + --(3);
3. y = 2 + 2 + 2;
4. y = 6
After 2 decrements and 1 increment, x becomes 2.

What is the value of the variable result after the following code is executed?

int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
}
else
{
result = 0;
}
count = count + 2;
}
System.out.println("Result: " + result);
Can someone explain why the answer is 30?
count starts at 0 and number 2 is added to it at each iteration. The loop stops when count is equal or higher than 10. Therefore the values of count will be :
0, 2, 4, 6, 8, 10
These are all even numbers so count % 2 == 0 will be true.
result starts at 0 and at each iteration count is added to it. Therefore the final result will be the sum of all the above numbers. And
0 + 2 + 4 + 6 + 8 + 10 = 30
To start with, count is 0. 0 % 2 == 0, so the if block is executed (and not the else block). result + count evaluates to 0, so result is set to 0. Repeat with 2 (count + 2) and this is added to result, as 2 % 2 == 0.
For every value of count, count % 2 == 0 evaluates to true. This is because you are incrementing it by 2 each time, so it will always remain even. Because of this, the else block is completely redundant and should be removed. Anyhow, what you are doing is summing all even numbers up to 10 (inclusive, as you use <=). This is 2 + 4 + 6 + 8 + 10 = 30, which is printed.
Side note: assignments which look like this (where v is a variable , o is an operator and e is an expression) may be simplified:
v = v o e;
is essentially (can anyone link me that question explaining the difference involving casting?) equivalent to
v o= e;
for arithmetic operators.
I just added two lines of code into yours and the code is now self-explaining.
int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
System.out.println("count%2 " + count%2 + " and Count is " + count + " and Result is " + result);
}
else
{
result = 0;
System.out.println("count%2 " + count%2 + " Count is " + count + " and Result is " + result);
}
count = count + 2;
}
System.out.println("Result: " + result);
and it prints to the console following output
count%2 0 and Count is 0 and Result is 0
count%2 0 and Count is 2 and Result is 2
count%2 0 and Count is 4 and Result is 6
count%2 0 and Count is 6 and Result is 12
count%2 0 and Count is 8 and Result is 20
count%2 0 and Count is 10 and Result is 30
Result: 30
I hope this helps.
It simply increments result by every even number from zero to 10. And by adding those numbers together, you get, well, 30.
Even numbers between 0 and 10 include:
0, 2, 4, 6, 8, 10
by adding them all up, you get:
0 + 2 + 4 + 8 + 10 = 30
You can even prove this by keeping track of result as you go along. See below.
int count = 0, result = 0;
do {
if (count % 2 == 0) {
System.out.println("Result went from " + result + " to " + (result += count));
continue;
}
result = 0;
System.out.println("Result is now " + 0);
} while ((count += 2) <= 10);
System.out.println("Result: " + result);

Counting number of comparisons in code

for(int i = 0; i < N * N; i = i + 1)
for(int j = 1; j < i + 1; j = j + 1)
junk = junk + 1;
I need to determine the relationship between the number of < operations executed and N. I should give an exact answer, such as 27N + 18.
Any help is appreciated! Thanks
For the first loop, as you can see i starts from 0 and goes to N^2 -1
1) That means N^2 + 1 times.
For every i, inner loop starts with 1 goes to N^2
2) 1 + 2 + 3 ... (N^2) = N^2 * (N^2 +1 ) / 2 = (N^4 + N^2) / 2
That means, operator "<" executed sum of 1 and 2.
N^2 + 1 + (N^4 + N^2) / 2 = (N^4 + 3N^2 + 2) / 2
= **(N^2 + 2) (N^2 + 1) / 2**

Iteration expression i += i

I have just started learning Java and pretty much programming and saw the following for loop expression:
for (int i = 1; i < 100; i += i)
System.out.print(i + " ");
My understanding is that "i += i" is short for "i = i + i". The output of the loop is "1, 2, 4, 8, 16, 32, 64". Cannot get my head around the iteration when "i" is 3 and higher. How can it become 8 etc.?
Because it's
Iteration 1 => 1+1
Iteration 2 => 2+2
Iteration 3 => 4+4
Iteration 4 => 8+8
etc.
With this particular for loop, the print statement is not being executed when i is 3 because i increments from 2 to 4. Let's take a closer look:
for (int i = 1; i < 100; i += i)
// iteration 1
System.out.print(i + " ");// prints 1 increment (1 += 1) == 2
------------------------------------------------------------------
// iteration 2
System.out.print(i + " ");// prints 2 increment (2 += 2) == 4
------------------------------------------------------------------
// iteration 3
System.out.print(i + " ");// prints 4 increment (4 += 4) == 8
------------------------------------------------------------------
// iteration 4
System.out.print(i + " ");// prints 8 increment (8 += 8) == 16
------------------------------------------------------------------
As you can see, i becomes 8 because on the third iteration of the loop, i is 4 and increments by itself to become 8.
Hope it helps :-)
first increment time : 1 + 1 = 2
second time : 2 + 2 = 4
third time : 4 + 4 = 8
forth time : 8 + 8 = 16 ... and so on 32,64,128,256....
Hope this helps you to clear your doubt.
In expression x+=y means really that you are writing x=x+y. In your expression i+=i really means that you are writing i = i+i which is equivalent to i = 2*i.
Now how for is working...It's calculating int i = 1 only once. Then it's checking your loop condition i < 100. The next step is to calculate next iteration value which will be i = 1 + 1 =2.
The 3rd iteration will calculate value for i in such way - i = 2 + 2 =4.
The 4rd iteration will be i = 4 +4 =8
....
If you want just to add by one, then please change i +=i by ++i.
for (int i = 1; i < 100; i += i)
System.out.print(i + " ");
Equivalent to:
int i = 1;
while(i<100){
System.out.print(i + " ");
i +=i;
}
Just to for completeness: i += K; is not exactly i = i + K; It is i = (TypeOf i ) (i + k)

Categories

Resources