0
public class Task {
  public static void main(String args[]) {

    int x = 0, p = 0, sum = 0;

    p = 1;
    x = 2;
    double q;
    sum = 0;
    while (p < 12) {
      q = x + p - (sum + 5 / 3) / 3.0 % 2;
      sum = sum + (x++) + (int) q;
      System.out.println(sum);
      if (x > 5)
        p += 4 / 2;
      else
        p += 3 % 1;
    }
    sum = sum + p;
    System.out.println(sum);
  }
}

While proceeding to line 12 (sum = sum + (x++) + (int)q;) i thought sum should be 5 but actually the output is 4. I tried line 12 in the interactions pane and indeed saw that sum=4. I don't get it. Shouldn't x++ yield 3 (x=2) and if this gets added to (int) q ( double q gave me sth like 2.666666), i should be getting 5. Can someone explain to me what happened?

Additionally,after getting my first output, how should I proceed? The next condition is:

if (x > 5)
  p += 4 / 2;
else
  p += 3 % 1;

since x<5, i should go for the else condition, right? My last question is that, after using p += 3%1, my p still remains 1, so do i return back to this loop (since p<12) or do I get out of this loop and proceed to line19? I'm not sure what to do.

Alexey
  • 1,198
  • 1
  • 15
  • 35
Hades
  • 11
  • 1
  • x will increment after the attribution of sum. – Gunner Mar 28 '16 at 12:55
  • thanks for the feedback. But I still don't get why sum=4 and not 5. Can you explain a little further? – Hades Mar 28 '16 at 13:51
  • x++ (the incrementation) is done after the current instruction, so x is 2 when the sum is calculated. After that, it will become 3. – Gunner Mar 28 '16 at 13:56
  • If you use pre incrementation (++x), like Alexey said, the incrementation will be done before calculating the sum, so the result will be 5. – Gunner Mar 28 '16 at 14:03
  • oh thanks! I keep getting confused with the pre/post incrementaton. Now i get what's happening. One more thing,can you tell me what happens after i calculate sum? Afterwards, p still remains as 1, does that mean i keep going back inside this while loop? If p doesn't equal 12, i won't be able to leave this loop. I'm not sure if I'm thinking this through right. – Hades Mar 28 '16 at 14:04
  • After 3 iterations, I think, x will become bigger than 5, so p will increase by 2 each time from now on (x>5 -> p=p+2). Eventually p will become 12, or bigger and will leave the while loop. – Gunner Mar 28 '16 at 14:09
  • okay. i finally get what's going on. Since i basically kept getting my pre/post increments wrong, i got all sorts of bad output. woahh, i'm really happy coz i've been stuck with this tracing problem for a while now. – Hades Mar 28 '16 at 14:20
  • You're welcome! You should, though, follow VinhNT advide and learn how to use a debugger. It's not that hard and is very useful. – Gunner Mar 28 '16 at 14:21

2 Answers2

1

In line 12 you are using post increment (x++). You should use pre increment ++x.

Post increment puts the current value of x in your statement, then increases x.

Pre increment initially increases x and after that puts result into your statement.

Community
  • 1
  • 1
Alexey
  • 1,198
  • 1
  • 15
  • 35
0

At your first time, 3%1=0 p +=3%1 => p+=0 thats why p still remains 1

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
VinhNT
  • 1,091
  • 8
  • 13
  • The thing is I did figure out that p=1 but basically I have confusions regarding line 12, which is asks me for the sum. I get 5 but it's actually 4. Do you know what's happening up there? – Hades Mar 28 '16 at 12:48
  • And after i get p=1, do I go back to this while loop? how do I get out of this while loop? It looks as if p will keep remaining as 1. – Hades Mar 28 '16 at 12:49
  • After 4 first loops, x will be 6, then, p will be re-assign in statement p+=4/2 which gives p+=2; then p will be 3,5,7,9,11,13 for each time the loop pass. When p=13, the loop is terminate – VinhNT Mar 28 '16 at 14:12
  • @Hades, I think you should learn how to debug in a IDE tool like eclipse/netbean to run step by step and see how it works – VinhNT Mar 28 '16 at 14:13
  • Alright, thanks, I understand what's happening now.Yeah. i kinda avoided going for debugging even though I keep seeing it's important that I learn by now how to debug. – Hades Mar 28 '16 at 14:23
  • It is not quite hard, see this tutorial http://www.vogella.com/tutorials/EclipseDebugging/article.html – VinhNT Mar 28 '16 at 14:29
  • Okay, I'll definitely check out the tutorial! – Hades Mar 28 '16 at 14:49