init() lesson 3
DESCRIPTION
ACM Init() Day 2TRANSCRIPT
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 canWe 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 LoopA 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 OutputAHHHHHHHHHHHHHHHHHHHHH 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 SidenoteYou might have seen that in loops I was using += and -=
These are just shorthand.
For LoopA '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?
Answer1 2 3 4 5 6 7 8 9
This is exclusive because 10 is not printed out.
What will this print out?
Answer1 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 indicesWhat if we have the array {15, 27, 21, 13, 44, 17}?
my_array = [15, 27, 21, 13, 44, 17]
Accessing Array ElementsHow 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 AccessThere 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 SortingWhat 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!