Java For Simple Code - Output Clarification

Can someone explain to me how this code prints 8 2 as a result?

 public class Check{ public static void main(String args[]){ int x=0; int y=0; for(int z=0;z<5;z++){ if(++x>2||++y>2){ x++; } } System.out.println(x+" "+y); } } 
+7
source share
8 answers

How to understand unknown code fragments:

  • Edit the code correctly. This helps a lot when reading code, and it is a simple and safe operation to get used to the code.

  • Try to find another way to express the same code in a simpler way. Run the code after each change to make sure your changes didnโ€™t break anything.

    This means replacing ++x with x=x+1 in the right place.

  • Unpack nested / complex if() conditions.

Thus, the rewritten code may look like this:

 for(int z=0; z<5; z ++) { x = x + 1; if( x > 2 ) { // first half of the OR x = x + 1; } else { // first half of the OR is false, we end up here y = y + 1; // pre increment if( y > 2 ) { x = x + 1; } } } 

Another good solution is to use a human debugger:

 for(int z=0; z<5; z++) { System.out.println("x="+x+", y="+y+", z="+z); // add this line in several places to see what happens ... } 
+3
source

In iteration 0, 1, the condition is not true, so it ends with x=2, y=2 .

Starting from iteration 2, the first condition ++x>2 true, so the second is not evaluated again. y remains fixed at 2. For each next cycle, x increases twice (after evaluating ++x>2 , once x++; ). So x becomes 4, 6 and 8.

+10
source

++x is pre increment of x . Your rating will look something like this.

 INITIAL VALUES: x=0, y=0 z=0, INVARIANT FOR z: z<5 () WHEN z=0 CHECK IF 1>2 || 1>2 THEN x++ WHEN z=1 CHECK IF 2>2 || 2>2 THEN x++ WHEN z=2 CHECK IF 3>2 || 2>2 y cond is not eval as x cond satisfied THEN x++(inc 3++) WHEN z=3 CHECK IF 5>2 || 2>2 // y cond is not eval as x cond satisfied THEN x++(inc 5 by 1) WHEN z=4 CHECK IF 7>2 || 2>2 //y cond is not eval as x cond satisfied THEN x++(inc 7 by 1) THUS : FINAL OUTPUT x= 8, y=2 
+6
source

It's all about how you have ++ x / ++ y in the if check. Follow the logic:

x = 0 y = 0 z = 0

Then you get into the if line, since ++ x is 1, it is not> 2, so it launches the ++ y part, which is y = 1, if the result is false, and your values โ€‹โ€‹now:

x = 1 y = 1 z = 0

iteration 2 is performed, with z = 1 Then you get into the if line, since ++ x is 2, not> 2, so it starts the ++ y part, which is y = 2, if the result is false, and your values โ€‹โ€‹are now :

x = 2 y = 2 z = 1

iteration 3 is executed, with z = 2 Then you get to the if line, since ++ x is 3, this is> 2, so x ++ does (does x = 4), ++ y is NOT executed because OR was true in if. Now the values โ€‹โ€‹are:

x = 4, y = 2, z = 2

Repeat this until z = 5 and you get x = 8, y = 2

+4
source

Intial loop execution

z=0 --> x=1, y=1

z=1 --> x=2, y=2

In the above case, the if condition is false.

Also, note that for each valid condition, if, x is incremented by 2 (pre-increment and post-increment).

Why did y = 2 stay the same ..? Logical operator '||' execution, if the first condition is true, it will not look or be satisfied for another condition

z=2 --> x=4, y=2

z=3 --> x=6, y=2

z=4 --> x=8, y=2

x = 8 --- and y = 2

+1
source

in an 'if' expression, for example:

 if (conditionA() || conditionB()) {...} 

if conditionA () returns true, condition B () is not called at all, because there is no need, the entire if statement must be true.

in detail: the cycle runs 5 times.

1st time: x and y are incremented to 1 in the if () statement.

2nd time: x and y are incremented to 2 in the if () statement.

Third time: x is incremented by 3, and this is enough to initiate execution inside the "if" block. y does not increase again because there is no need to evaluate the expression after || (OR) if the first part is true.

4th time: x is incremented by 5, if () is still true, so x is incremented to 6 again.

Fifth time: x increases by 7 in if () and 8 in the block.

The end result: x is 8, y is 2.

+1
source

Since the System.out.println () statement is outside the loop, the println statement is run only once. Each iteration of the loop, the if statement checks if x and y are greater than 2; however, at first it increases both values. The first three times through the loop the conditions are not met, but x and y are still increasing. Each time after this, x and y both increase by one; however, since the condition is fulfilled, x increases by extra time. In short, this is what x and y look after each iteration:

 zxy -- -- -- 0 1 1 1 2 2 2 4 3 3 6 4 4 8 5 

EDIT: due to the short circuit logic in the if statement (||), only the first part of the expression is evaluated, and y never increases. As a result, y would have only 2 left.

0
source

The cycle starts 5 times. When its approaching, if x and y will receive an increment of 1 during the first 2 times of his own. At time x & y are 2, but the if condition is not met. For the 3rd time, x will be 3, and it will not check y, so y will not increase, and it will go inside if it increases again. Thus, y will be equal to 2. But x will increase by 2 * 2 = 4 times, so x = 8 and y = 2

0
source

All Articles