Exercise 32: Loops and Arrays
You should now be able to do some programs that are much more interesting. If you have been keeping up, you should realize that now you can combine all the other things you have learned with if-statements and boolean expressions to make your programs do smart things.
However, programs also need to do repetitive things very quickly. We are going to use a for-loop in this exercise to build and print various arrays. When you do the exercise, you will start to figure out what they are. I won't tell you right now. You have to figure it out.
Before you can use a for-loop, you need a way to store the results of loops somewhere. The best way to do this is with a array. A array is exactly what its name says, a container of things that are organized in order. It's not complicated; you just have to learn a new syntax. First, there's how you make a array:
hairs = ['brown', 'blond', 'red'] eyes = ['brown', 'blue', 'green'] weights = [1, 2, 3, 4]
What you do is start the array with the [ (left bracket) which "opens" the array. Then you put each item you want in the array separated by commas, just like when you did function arguments. Lastly you end the array with a ] (right bracket) to indicate that it's over. Ruby then takes this array and all its contents and assigns them to the variable.
This is where things get tricky for people who can't program. Your brain has been taught that the world is flat. Remember in the last exercise where you put if-statements inside if-statements? That probably made your brain hurt because most people do not ponder how to "nest" things inside things. In programming this is all over the place. You will find functions that call other functions that have if-statements that have arrays with arrays inside arrays. If you see a structure like this that you can't figure out, take out a pencil and paper and break it down manually bit by bit until you understand it.
We now will build some arrays using some for-loops and print them out:
You should immediately see that Ruby has two kinds of loops that I am calling a for-loop. In programming the term for-loop just means "a loop that goes through each thing in a array of things". In ruby this is both for number in the_count style, and the more common fruits.each style. You should use the .each version as it is more reliable and what other Ruby programmers expect you to write.
Ruby programmers are very particular about how their for-loops are written and will declare you a bad programmer for simply using this one construct wrong. They went so far as to break for-each version of the loops so that there are problems with using it, forcing you to conform to their culture. Heed my warning that you should always use .each and never for-each for fear of being forever branded bad and shunned. Yes, it is as ridiculous as it sounds.
What You Should See
$ ruby ex32.rb This is count 1 This is count 2 This is count 3 This is count 4 This is count 5 A fruit of type: apples A fruit of type: oranges A fruit of type: pears A fruit of type: apricots I got 1 I got pennies I got 2 I got dimes I got 3 I got quarters adding 0 to the list. adding 1 to the list. adding 2 to the list. adding 3 to the list. adding 4 to the list. adding 5 to the list. Element was: 0 Element was: 1 Element was: 2 Element was: 3 Element was: 4 Element was: 5
- Take a look at how you used (0..5) in the last for-loop. Look up Ruby's "range operator" (.. and ...) online to see what it does.
- Change the first for number in the_count to be a more typical .each style loop like the others.
- Find the Ruby documentation on arrays and read about them. What other operations can you do besides the push function? Try << too, which is the same as push but is an operator, like fruits << x is the same as fruits.push(x).
Common Student Questions
- How do you make a 2-dimensional (2D) array?
- That's a array in a array like this: [[1,2,3],[4,5,6]]
- Aren't arrays and arrays the same thing?
- Depends on the language and the implementation. In classic terms a array is very different from an array because of how they're implemented. In Ruby though they call these arrays. In Ruby they call them arrays. Just call these arrays for now since that's what Ruby calls them.
- How come a for-loop can use variables that aren't defined yet?
- It defines that variable, initializing it to the current element of the loop iteration, each time through.