while loop executes the code it contains as long as
its conditional statement remains true. The following piece of code initializes a counter
0 and sets up
an array containing four elements called
breeds). It also creates a temporary array named
(You'll learn more about arrays in Chapter 6.)
The following few paragraphs are fairly fundamental, and are provided for beginning programmers. If you already have plenty of programming under your belt, skip ahead to the code itself.
while loop will execute as long as its
i < breeds.size) is true. The
i variable starts out its little life equaling
0, and the size or length of the
breeds array is
As you come to the end of the loop,
i is incremented
by 1, and then control returns to the top of the loop. In the first loop,
0, and is fed as
0 as an argument to
breeds[i], which retrieves the first element (numbered
0). This is the string value
element is appended via
<< to the
temp array. The
Quarter. At this point,
1 is added to
i by the
+= operator, so
equals 1. And we take it again from the top.
This continues until
4, whereupon the conditional test for
while fails. The Ruby interpreter moves to the next valid statement, that is,
temp.sort!, which sorts the new array alphabetically.
It does not make a copy but changes the array in place. You know this by the tell-tale
! at the end of the method name ( ...