Learning starts with a question and continues with a discovery Stay hungry for knowledge.
Stephan van Hulst wrote:What do you mean by "reading", or "read it all in one go"?
The java compiler translates code into byte code, which pretty much is a very simple set of instructions like "move data from memory address 'a' to memory address 'b'" and "jump to the instruction at line 'c' if the data in register 'd' equals 0". This code is then executed instruction by instruction by the Virtual Machine, which may do a number of fancy optimizations such as reordering lines, pipelining or even predicting instructions, etc. It's really quite complex.
Note that a line such as x++; may actually be translated into multiple instructions.
Zukky Bee wrote:
How can the value of y be "3" in "23"? y=y+x = which is 1 + 1 (from previous value), which equates to 2. So surely the answer should be 2 (i.e. 22)? However, it seems like x=x+1 is firing first, then the y=y+x is firing second. Thus making x=2 and y=1 equating to 2 + 1 = 3 (i.e. 23). Do you understand? It seems to have an order of code execution. This is what I am trying to understand?
Henry Wong wrote:
Zukky Bee wrote:
How can the value of y be "3" in "23"? y=y+x = which is 1 + 1 (from previous value), which equates to 2. So surely the answer should be 2 (i.e. 22)? However, it seems like x=x+1 is firing first, then the y=y+x is firing second. Thus making x=2 and y=1 equating to 2 + 1 = 3 (i.e. 23). Do you understand? It seems to have an order of code execution. This is what I am trying to understand?
If your point of reference is between the print statements, then yes, "x = x + 1" is firing first... think about it, your code prints output, then, it does "x = x + 1", loops around to the top of the loop, and then "y = y + x".
Henry
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
fred rosenberger wrote:
This code fires in order, over and over. so you get here with both x and y set to 0.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 0, or 0.
We print "00"
we print "x = 0 y = 0 "
we set x to whatever x + 1 equals, or 0 + 1 or just 1, so x is now 1.
we loop back to the top.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 1, or 1.
We print "11"
we print "x = 1 y = 1 "
we set x to whatever x + 1 equals, or 1 + 1 or just 2, so x is now 2.
we loop back to the top.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 1 + 2, or 3.
We print "23"
we print "x = 2 y = 3 "
we set x to whatever x + 1 equals, or 2 + 1 or just 3, so x is now 3.
we loop back to the top.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 3 + 3, or 6.
We print "36"
we print "x = 3 y = 6 "
we set x to whatever x + 1 equals, or 3 + 1 or just 4, so x is now 4.
we loop back to the top.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 6 + 4, or 10.
We print "410"
we print "x = 4 y = 10 "
we set x to whatever x + 1 equals, or 4 + 1 or just 5, so x is now 5.
we loop back to the top.
Since x is NOT less than 5, we break out of the loop.
Zukky Bee wrote:
WHY is it firing "x=x+1" first is my question? y=y+x is at the top? Surely it should do that first, then x=x+1? Does Java execute from top line and then down (i'm guessing not because of the way the code is being executed?)
Zukky Bee wrote:Question here is  does Java read all of its code from topdown?
Question here is  does Java read all of its code from topdown?
On top of spaghetti all covered in cheese, there was this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn KlassenKoop
https://coderanch.com/wiki/718759/books/BuildingWorldBackyardPaulWheaton
