• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does the compiler arrive at the answer ?  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: added indentation]

For the above code, the compiler gives the answer of a-b c-d

How does it arrive at this answer ? I think my understanding is flawed. Please correct me on my understanding below.

I assume that Java starts counting from 0..

So when
number = 0 , result = none as 0 doesn't fall within the while clause and there are no statements for occurrences not within the while clause.
number = 1, result = d due to if (x == 1) clause.
number = 2, result = -b c due to x = x - 1; System.out.print(“-”); ( 3-1=2 ? ) and the if (x == 2) clause.
number = 3, result = a due to if (x > 2) clause.

Therefore, answer is d-b ca ?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't execute the code, it "simply" takes the source code in a .java file and validates and transforms it into byte code that ends up in a .class file
Verifying and executing the byte code is the job of the Java Virtual Machine (JVM) runtime.

The execution path is somthing like this:

- Variable x is assigned a value of 3
- The while-loop's condition is evaluated and is true, as x has a value of 3, which is greater than 0.
- The while loop's body is executed for the first time (first iteration).
- The first if-statement's condition is evalutated and is true as x has a value of 3 which is greater than 2.
- The if-statement's body is executed, which prints the value "a".
- Variable x is re-assigned it's current value minus 1, which makes its new value 2.
- Value "-" is printed.
- The second if-statement's condition is evaluated...

See if you can pick it up from there.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have initialized x to 3, so in first iteration it is printing
a-b c
then appending
-d in second iteration.

Think once again.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:The compiler doesn't execute the code, it "simply" takes the source code in a .java file and validates and transforms it into byte code that ends up in a .class file
Verifying and executing the byte code is the job of the Java Virtual Machine (JVM) runtime.

The execution path is somthing like this:

- Variable x is assigned a value of 3
- The while-loop's condition is evaluated and is true, as x has a value of 3, which is greater than 0.
- The while loop's body is executed for the first time (first iteration).
- The first if-statement's condition is evalutated and is true as x has a value of 3 which is greater than 2.
- The if-statement's body is executed, which prints the value "a".
- Variable x is re-assigned it's current value minus 1, which makes its new value 2.
- Value "-" is printed.
- The second if-statement's condition is evaluated...

See if you can pick it up from there.


Oh thanks alot =D .

When does the looping stop ? It seems like it doesn't stop at the 1st false condition it encounters, otherwise the d wouldn't be printed.

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before a while-loop begins a new iteration its condition is evaluated. If it evaluates to true the body is executed, if it doesn't the while-loop exits.

In this example, before the first iteration the value of x is 3, so the while conditon x > 0 is true and the first iteration starts during which the body of the while-loop is executed. When the end of the while-loop's body is reached the first iteration is complete, but the while loop isn't done yet. Its condition is evaluated once again and this time x has a value of 2, because it was decremented by 1 during the first iteration by the code at line 8. When the second iteration completes x has value of 0, because x was decremented by 1 again at line 8, and this time also at line 15, because the if-condition was true. The while-loop's condition is evalutated for a third and final time, and this time it evaluates to false, because 0 is not larger than 0. The while loop is now done, after three evaluations of its condition and two iterations of its body.

If you play aroud with the starting value of x, you'll get different behavior. Try it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!