1

What does each expression evaluate to? Assume x is 2 before each one.

  • int num = x++ * 3;
    So this would be equivalent to (2)*3 or num=6 and x is now 3.

  • num *= x;
    num =2*2 or 4

  • (x < 2) && (x > 1)
    Becomes FALSE, because (2<2)=false and (2>1)=true so it's false.

  • (++x < 2) || (x < 1)
    (3<2) is false and then ((2+1)<1) is also false, so it's false?
    One question is in this case, is the preincrement applied to the variable before the break? Should the second x value be 3 or 2?
    I also have the same question for postincrement. Let's say I have num=x++ *x++ where initial x=2. So is this 2*2 or 2*3?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Chris
  • 1,881
  • 3
  • 20
  • 27
  • Which "break" are you talking about? – Felix Kling Mar 01 '12 at 01:43
  • On the fourth one. Since the preincrement is in the first bracket thing (im sure it has a special name) does it effect the value of the x in the second parenthesis comparison? – Chris Mar 01 '12 at 01:45
  • Like if the value of x is pre/post incremented in a first half of an AND or OR statement, does it affect the value of the second half's value? – Chris Mar 01 '12 at 01:45
  • 5
    Why don't you try it out yourself? --> [Ideone](http://ideone.com) – JRL Mar 01 '12 at 01:47
  • This question might help: [Pre & post increment operator behavior in C, C++, Java, & C#](http://stackoverflow.com/questions/6457130/pre-post-increment-operator-behavior-in-c-c-java-c-sharp). – Felix Kling Mar 01 '12 at 01:48
  • I don't have admin rights to install Eclipse nor the SDK, but thanks to JRL I can do my Java testing now. Really cool program! – Chris Mar 01 '12 at 01:57

5 Answers5

1

It's incremented before the "break" yes. Basically it's the first thing java does (parenthesis are still the first ones actually). So in (++x < 2) || (++x < 3) the 2nd ++x happens after the first one if it isn't true.

fbernardo
  • 10,016
  • 3
  • 33
  • 46
  • I realize it adds one to the value of the x for the first half before executing the statement. Let me try to explain this a different way: `var x=2;`
    `(x++ < 4)&&(x==2);`
    Is the second statement true or false. I am wondering whether preincrement affects the entire line's x value..
    – Chris Mar 01 '12 at 01:48
  • @Chris: Why don't you just run it? If the result is `false`, the second statement is `false`. If it is `true`, it's `true`. That's easy to find out. – Felix Kling Mar 01 '12 at 01:50
  • @Felix Kling: I don't have admin rights to the comptuer I am on to install either Eclipse or the SDK. Although someone posted this cool online java runner thing I am using now. I've never done it before, so how can you return the value of the OR statement without writing a large if else thing? – Chris Mar 01 '12 at 01:56
  • @fbernardo if the program gives me this error: "cannot return a value from method whose result type is void" that means the statement is false right? Or does it mean something is actually wrong? – Chris Mar 01 '12 at 02:12
  • If you declare a void method (like main) you can't return a value. That's what a void method is, a method that doesn't return anything... So, that doesn't mean it's true or false. – fbernardo Mar 01 '12 at 02:13
  • Btw, if you want to return the result of a boolean expression like "a | b" the return type is "boolean". – fbernardo Mar 01 '12 at 02:15
1

int num = x++ * 3; => OK: x = 3, num = 6
num *= x; => what's the initial value of num ? if num = 2, then you are also OK.
(x < 2) && (x > 1) is false when x = 2, OK
(++x < 2) || (x < 1) is false when x = 2, OK as well

I remember I had a look at the openJDK, especially the Lower class in javac source and ++x is translated into x += 1 therefore you can see it as:
((x += 1)) < 2) which is (whithout type casting): ((x = (x + 1)) < 2). The second test will have the newer x value i.e 3 because java evaluates the conditions from left to right.

Jerome
  • 4,472
  • 4
  • 18
  • 18
0

Yes those are some very basic concepts, do yourself a favour. Download unittest framework something like jUnit would do. Then you can run all these tests quickly with a few asserts that will show you exactly what is going on. Another way to do it without much overhead is just to print it out on the console.

int num = x++ * 3;
System.out.println( "num=" + num );  // num=6
Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40
  • I don't have admin rights to the comptuer I am on to install either Eclipse or the SDK. Although someone posted this cool online java runner thing I am using now. – Chris Mar 01 '12 at 02:00
0
num *= x;
num =2*2 or 4

I don't know where you got that.

num *= x; is equivalent to num = num * x;. If num was 6 (from the previous statement) then it would now be 12 (assuming x is still 2).

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0
  1. "Assume x is 2 before each one." Is that a given assumption or is it an assumption you're making? I don't believe that you can make that assumption. I believe that you have evaluate all of these statements in sequence.
  2. What is the expanded form of a *= b?
  3. What is the initial value of num when you're at the second statement?
  4. On your last statement you did correctly (assuming your initial conditions are correct) compute (2+1) < 1 because x has been incremented.

Keep track of the values of x and num after each statement and I think you'll get to the right answer.

Good luck!

Pradeep Gollakota
  • 2,161
  • 16
  • 24