5. Can we improve this?
• This calculator is pretty inconvenient
• User must be prompted often
• Unfriendly interface
• Only one calculation can be performed
• Can this be improved?
6. Of course we can
We can use 'loops' to make a much better calculator
7. Why is this better?
• We can do more than one calculation
• The user can type in a legitimate mathematical
equation
• There is less confusion and clutter in the interface
8. Loops
• Loops are a great tool if you want to repeat an
action
• We are going to go over three types of loops:
'while', 'until', and 'for'
9. While Loop
A while loop checks to see if a certain condition is true, and
while it is, the loop keeps running. As soon as the condition
stops being true, the loop stops.
while statement is true
do something
end
22. A more fun example
Let's go to koding.com and run it to see what it does!
23. Danger!
We always have a condition that will make the loop end.
What if we forget to update the condition?
We might create something called an infinite loop. This loop
will keep running forever, which is really bad.
25. Until Loop
An 'until' loop will run until a set condition is met. Once the
condition is met the loop stops running.
until condition
do something
end
40. A Brief Sidenote
You might have seen that in loops I was using += and -=
These are just shorthand.
41. For Loop
A 'for' loop is used when we know how many times we'll be
looping when we start. The loop will run however many times
we tell it to.
for variable in x...y
do something
end
50. More about for loops
• Notice that we typed in 'for num in 1...4' and 1, 2, 3
was printed out
• 4 was not printed out
• That is because we typed in 1...4
• How do we make the same loop print out 1, 2, 3, 4?
51. Inclusive and Exclusive
Ranges
• There are two different ways to write for loops in
Ruby
• If we type in 'for num in 1...4' it will print out 1, 2, 3
(Exclusive)
• If we type in 'for num in 1..4' it will print out 1, 2, 3, 4
(Inclusive)
• Notice the different numbers of '.'
55. Answer
1
2
3
4
5
6
7
8
9
10
This is inclusive because 10 is printed out.
56. Looping Tools
• Some loops we write are fairly complex and include
multiple conditional statements
• What if we want to skip an iteration of the loop
depending on a condition?
• There is a command to do this: the 'next' command
• If a 'next' is reached Ruby will immediately
advance to the next iteration of the loop
60. Arrays
• An array is a new data type
• In simplest terms, an array is a
list
• You can have an array of any
type of variable: string,
numbers, booleans, etc
• Arrays work great for storing
longs lists of variables!
61. Accessing Array Elements
• Once we make our array we want to be able to use
each individual element. To do this we can look at
the index of each array element.
• Each element of the array is assigned a number
from 0...array.length-1
• The first element gets index 0, the next gets index
1, the one after that gets index 2, and so on.
62. Array indices
What if we have the array {15, 27, 21, 13, 44, 17}?
my_array = [15, 27, 21, 13, 44, 17]
63. Accessing Array Elements
How do we actually get to a specific element of an array? Let's
try to get the third number of the following array:
The third number's index is 2. Therefore, to get the third
element of this array we would type in 'my_array[2]'
64. General Array Access
There is a general formula for accessing array elements:
array_name[element# - 1]
For example, if we have an array called 'string_array' and we
want to print out the fourth element we would type:
Will print out 'cheese'
65. Intro to Sorting
What if we get a large array of numbers and we want to put
them in ascending order?
Ruby has a method for that!
The '.sort!' method will rearrange elements of an array so they
are in order!
66. Sort example
But wait! These are numbers! What if we want to sort an array
of strings?
67. Sorting String Arrays
Calling '.sort!' on an array of strings will also work! The array
will be sorted alphabetically.
68. What we did today
• While Loops (Be careful about infinite loops!)
• Until Loops
• For Loops
• Next
• Arrays
• Simple Sorting
70. Homework: Fizzbuzz
Write a program that prints the numbers from 1 to 100. For
multiples of three print “Fizz” instead of the number and for the
multiples of five print “Buzz”. For numbers which are multiples
of both three and five print “FizzBuzz”.
The next slide shows what the output should look like for the
first 26 numbers.
Remember, if you have any questions you can ask on the
Facebook page!