When we look at looping constructs, for-loops will iterate through values of an array, or though the number of values that are present or through a range of values as we define it. Sometimes, though, we need something a little more longer lasting, something that will allow us to keep on going as long as a condition is met. As soon as that condition is no longer met, then the loop should end, either to move onto to another part of a program, or to terminate a program altogether.
That's exactly what the while-loop does. So long as a condition is true, then the loop will keep executing. As soon as it is false, then it will stop. Hmmm, sounds kinda' "Boolean" to me, how about you :)? Think that Boolean truth table might be helpful in this exercise? I'm going to bet the answer is "yes"!
while-loops are pretty straight-forward. They perform a simple test to check if a code block will prove true, and if it does, it jumps to the top of the code block and repeats, and it will keep doing this until the expression it evaluates to keep the loop going is False.
The challenge is that, if we are not careful in how we program, or we don't set up good parameters, we could get a while loop that will run forever. Note, that's not a bad thing, we have programs do it all the time (a web browser or a 32 bit application alike, if you let a while-loop run indefinitely, that's a good bet it will keep doing that).
Zed recommends that programmers keep the following in mind when using while-loops top keep them out of trouble as much as possible:
- Make sure that you use while-loops sparingly. Usually a for-loop is better.
- Review your while statements and make sure that the thing you are testing will become False at some point.
- When in doubt, print out your test variable at the top and bottom of the while-loop to see what it's doing.
Cool thing is, this exercise does exactly those three things:
What You Should See
$ ruby ex33.rb
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
Extra Credit
Convert this while loop to a function that you can call, and replace 6 in the test (i < 6) with a variable. Now use this function to rewrite the script to try different numbers.
Add another variable to the function arguments that you can pass in that lets you change the + 1 on line 8 so you can change how much it increments by. Rewrite the script again to use this function to see what effect that has.
Now, write it to use for-loops and ranges instead. Do you need the incrementor in the middle anymore? What happens if you do not get rid of it?
[in my code, the incrementor does nothing if I keep it it or it I remove it, provided I am using a range, the for loop increments the range, and it ignores the incremental change inside of the loop]
TESTHEAD's TAKEAWAYS:
Once again, this is a good example that the tool for the job is the one that is best suited for it. It also shows that while-loops and for-loops are somewhat interchangeable, albeit with some slight differences.
1 comment:
I found this for extra credit 5: http://railsforum.com/viewtopic.php?id=19081
so...
def is_a_number?(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end
if is_a_number?(next_move.to_i())
should cover everything, I think.
Post a Comment