• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How does the compiler arrive at the answer ?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic