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.
Can anyone please please help me?
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.
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."
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 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 aweinspiring .
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.
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 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.
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."
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
All of the world's problems can be solved in a garden  Geoff Lawton. Tiny ad:
Rocket Oven Kickstarter  from the trailboss
https://coderanch.com/t/695773/RocketOvenKickstartertrailboss
