ACM init() 
Day 3: November 5, 2014
Any questions? 
• Getting user input 
• Control flow: if, else, elsif, unless 
• Making comparisons: ==, !=, <, >, <=, >= 
• Boolean operators: &&, ||, !
Back to Mad Libs 
...
Simple Calculator 
Let's look at the code 
in Koding so you 
can actually see it.
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?
Of course we can 
We can use 'loops' to make a much better calculator
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
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'
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
While example 
x = 1 
Output:
While example 
x = 1 
Output:
While example 
x = 1 
Output: 
1
While example 
x = 2 
Output: 
1
While example 
x = 2 
Output: 
1
While example 
x = 2 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
A more fun example 
Let's go to koding.com and run it to see what it does!
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.
Infinite Loop Output 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
...
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
Until example 
x = 3 
Output:
Until example 
x = 3 
Output:
Until example 
x = 3 
Output: 
3
Until example 
x = 2 
Output: 
3
Until example 
x = 2 
Output: 
3
Until example 
x = 2 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
What will this print out?
Answer 
0 
1 
2 
3 
4
A Brief Sidenote 
You might have seen that in loops I was using += and -= 
These are just shorthand.
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
For example 
x = 1 
Output:
For example 
x = 1 
Output: 
1
For example 
x = 2 
Output: 
1
For example 
x = 2 
Output: 
1 
2
For example 
x = 3 
Output: 
1 
2
For example 
x = 3 
Output: 
1 
2 
3
For example 
x = 4 
Output: 
1 
2 
3
For example 
x = 4 
Output: 
1 
2 
3
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?
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 '.'
What will this print out?
Answer 
1 
2 
3 
4 
5 
6 
7 
8 
9 
This is exclusive because 10 is not printed out.
What will this print out?
Answer 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
This is inclusive because 10 is printed out.
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
What will this print out?
Answer 
1 
3 
5 
7 
9 
This loop skipped if x was even and printed out x if it was odd.
Any questions on 
loops?
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!
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.
Array indices 
What if we have the array {15, 27, 21, 13, 44, 17}? 
my_array = [15, 27, 21, 13, 44, 17]
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]'
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'
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!
Sort example 
But wait! These are numbers! What if we want to sort an array 
of strings?
Sorting String Arrays 
Calling '.sort!' on an array of strings will also work! The array 
will be sorted alphabetically.
What we did today 
• While Loops (Be careful about infinite loops!) 
• Until Loops 
• For Loops 
• Next 
• Arrays 
• Simple Sorting
Any questions before we 
get to the homework?
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!
Init() Lesson 3

Init() Lesson 3

  • 1.
    ACM init() Day3: November 5, 2014
  • 2.
    Any questions? •Getting user input • Control flow: if, else, elsif, unless • Making comparisons: ==, !=, <, >, <=, >= • Boolean operators: &&, ||, !
  • 3.
    Back to MadLibs ...
  • 4.
    Simple Calculator Let'slook at the code in Koding so you can actually see it.
  • 5.
    Can we improvethis? • 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 wecan We can use 'loops' to make a much better calculator
  • 7.
    Why is thisbetter? • 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 • Loopsare 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 Awhile 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
  • 10.
    While example x= 1 Output:
  • 11.
    While example x= 1 Output:
  • 12.
    While example x= 1 Output: 1
  • 13.
    While example x= 2 Output: 1
  • 14.
    While example x= 2 Output: 1
  • 15.
    While example x= 2 Output: 1 2
  • 16.
    While example x= 3 Output: 1 2
  • 17.
    While example x= 3 Output: 1 2
  • 18.
    While example x= 3 Output: 1 2 3
  • 19.
    While example x= 4 Output: 1 2 3
  • 20.
    While example x= 4 Output: 1 2 3
  • 21.
    While example x= 4 Output: 1 2 3
  • 22.
    A more funexample Let's go to koding.com and run it to see what it does!
  • 23.
    Danger! We alwayshave 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.
  • 24.
    Infinite Loop Output AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH ...
  • 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
  • 26.
    Until example x= 3 Output:
  • 27.
    Until example x= 3 Output:
  • 28.
    Until example x= 3 Output: 3
  • 29.
    Until example x= 2 Output: 3
  • 30.
    Until example x= 2 Output: 3
  • 31.
    Until example x= 2 Output: 3 2
  • 32.
    Until example x= 1 Output: 3 2
  • 33.
    Until example x= 1 Output: 3 2
  • 34.
    Until example x= 1 Output: 3 2 1
  • 35.
    Until example x= 0 Output: 3 2 1
  • 36.
    Until example x= 0 Output: 3 2 1
  • 37.
    Until example x= 0 Output: 3 2 1
  • 38.
    What will thisprint out?
  • 39.
    Answer 0 1 2 3 4
  • 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
  • 42.
    For example x= 1 Output:
  • 43.
    For example x= 1 Output: 1
  • 44.
    For example x= 2 Output: 1
  • 45.
    For example x= 2 Output: 1 2
  • 46.
    For example x= 3 Output: 1 2
  • 47.
    For example x= 3 Output: 1 2 3
  • 48.
    For example x= 4 Output: 1 2 3
  • 49.
    For example x= 4 Output: 1 2 3
  • 50.
    More about forloops • 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 '.'
  • 52.
    What will thisprint out?
  • 53.
    Answer 1 2 3 4 5 6 7 8 9 This is exclusive because 10 is not printed out.
  • 54.
    What will thisprint out?
  • 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
  • 57.
    What will thisprint out?
  • 58.
    Answer 1 3 5 7 9 This loop skipped if x was even and printed out x if it was odd.
  • 59.
  • 60.
    Arrays • Anarray 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 Whatif 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 Butwait! 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 didtoday • While Loops (Be careful about infinite loops!) • Until Loops • For Loops • Next • Arrays • Simple Sorting
  • 69.
    Any questions beforewe get to the homework?
  • 70.
    Homework: Fizzbuzz Writea 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!