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.
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.
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.
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.
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."
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|