This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

Hello. I'm very new to Java and computer programing. I just learned how to do Hello World less then a week ago. I've been working my way through Head First Java. Everything was going quite well until i got to the end of chapter 1 puzzles, where one of the exercises just went right over my head. I can't think of an easy way to explain exactly what the exercise is but that's not really important. What is important is just I do not have a clue what is happening with this code and why I'm getting the output I'm getting. I know it's the right output as it's one of the answers in the book. But this exercise goes into things that the book just hasn't covered and expects me to know them. Here is the code...

Ok and the answer i'm getting when i run the program is 00 11 21 32 42

But how? What is going on. up until now the only thing the book has covered is how to do simple "while" and "if" loops, which i was finding quite easy. Basically i was doing things like x = 1 and while x = 1 it will print this statement 5 times ect. But with this code there are a few things i don't understand. First of all this is the first time i've typed in a code and numbers have come out. Up until now the only things the program would say is things i told it to.

So how am i getting these numbers? I wouldn't be so confused but surely if x = 0 and y = 0 and while x is smaller then 5 y = x - 1 that is basically saying 0 = 0 - 0.

Neil Cartmell wrote:So how am i getting these numbers? I wouldn't be so confused but surely if x = 0 and y = 0 and while x is smaller then 5 y = x - 1 that is basically saying 0 = 0 - 0.

Thank you Henry. But i haven't a clue what you mean or how that chart can help me work out what is happening in the code. I'm sure that would make it clear to a lot of people but I need it to be explained in terms I am more familiar with.

What i would really appreciate would be for you to talk me through the code just using words explaining what is happening in each part.

While I find Henry's table very helpful, I will try to explain it in words to assist:

x and y are given the value 0. We then hit the start of the while loop, with the condition the loop will run while x < 5. 0 is less than 5, so we jump into the loop. First, we assign y the value of x - y. Since x and y are both 0, it is 0 - 0, which equals zero, so y is still 0. After that we print out the values of x and y, giving you the first answer of 00. After that, we assign x the value of x +1. 0 + 1 is 1, so now x is 1.

Jumping back to top of loop:

x now equals 1. 1 is less than 5, so the loop runs again. We assign y the value of x - y. x is now 1, and y is still zero. 1 - 0 is 1, so y is now 1. We then print the values of x and y, which are 1 and 1. After that, we assign x the value of x + 1. Since x is 1, it is 1 + 1, which equals 2. Now x is equal to 2, and we go back to the start of the while loop to test again.

And this rotation continues until the while condition returns false. This loop will end, but note that it doesn't always have to be that way, as you could write code that never ends the loop, resulting in an infinite loop. For instance, if the condition was x > -1, this loop would go forever, theoretically.

SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150

posted

0

W. Joe Smith wrote:While I find Henry's table very helpful, I will try to explain it in words to assist:

x and y are given the value 0. We then hit the start of the while loop, with the condition the loop will run while x < 5. 0 is less than 5, so we jump into the loop. First, we assign y the value of x - y. Since x and y are both 0, it is 0 - 0, which equals zero, so y is still 0. After that we print out the values of x and y, giving you the first answer of 00. After that, we assign x the value of x +1. 0 + 1 is 1, so now x is 1.

Jumping back to top of loop:

x now equals 1. 1 is less than 5, so the loop runs again. We assign y the value of x - y. x is now 1, and y is still zero. 1 - 0 is 1, so y is now 1. We then print the values of x and y, which are 1 and 1. After that, we assign x the value of x + 1. Since x is 1, it is 1 + 1, which equals 2. Now x is equal to 2, and we go back to the start of the while loop to test again.

And this rotation continues until the while condition returns false. This loop will end, but note that it doesn't always have to be that way, as you could write code that never ends the loop, resulting in an infinite loop. For instance, if the condition was x > -1, this loop would go forever, theoretically.

Thank you that was brilliantly explained. I get it now. Phew! It wasn't quite as complicated as i thought. Thanks again.

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150

posted

0

One other thing i'm not sure of is this line

what are the quotation marks for? i don't understand why this line doesn't say

I know that's probably a stupid question.

W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710

posted

0

Neil Cartmell wrote:

Thank you that was brilliantly explained. I get it now. Phew! It wasn't quite as complicated as i thought. Thanks again.

I wouldn't say brilliant, just mearly awe-inspiring .

But seriously...the way I usually work with loops is I determine what the loop's condition is, then make sure I will enter the loop in the first place. After that, I figure out where in the loop that condition will change, and if it never does then it could be an infinite loop. Other than that, it is just a matter of going line by line to figure out what the code does inside the loop, and keeping track of changing values and whatnot from inside the loop as it repeats.

W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710

posted

0

Neil Cartmell wrote:One other thing i'm not sure of is this line

what are the quotation marks for? i don't understand why this line doesn't say

I know that's probably a stupid question.

Not at all! If you wrote

it would print the sum of x and y. By adding in + "" +, it makes it String concatenation rather than addition. I will leave you to Google java string concatenation.