init() lesson 3

71
ACM init() Day 3: November 5, 2014

Upload: ucla-association-of-computing-machinery

Post on 02-Jul-2015

245 views

Category:

Software


0 download

DESCRIPTION

ACM Init() Day 2

TRANSCRIPT

Page 1: Init() Lesson 3

ACM init()Day 3: November 5, 2014

Page 2: Init() Lesson 3

Any questions?

• Getting user input

• Control flow: if, else, elsif, unless

• Making comparisons: ==, !=, <, >, <=, >=

• Boolean operators: &&, ||, !

Page 3: Init() Lesson 3

Back to Mad Libs

...

Page 4: Init() Lesson 3

Simple Calculator

Let's look at the code in Koding so you

can actually see it.

Page 5: Init() Lesson 3

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?

Page 6: Init() Lesson 3

Of course we canWe can use 'loops' to make a much better calculator

Page 7: Init() Lesson 3

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

Page 8: Init() Lesson 3

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'

Page 9: Init() Lesson 3

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

Page 10: Init() Lesson 3

While example

x = 1

Output:

Page 11: Init() Lesson 3

While example

x = 1

Output:

Page 12: Init() Lesson 3

While example

x = 1

Output: 1

Page 13: Init() Lesson 3

While example

x = 2

Output: 1

Page 14: Init() Lesson 3

While example

x = 2

Output: 1

Page 15: Init() Lesson 3

While example

x = 2

Output: 1 2

Page 16: Init() Lesson 3

While example

x = 3

Output: 1 2

Page 17: Init() Lesson 3

While example

x = 3

Output: 1 2

Page 18: Init() Lesson 3

While example

x = 3

Output: 1 2 3

Page 19: Init() Lesson 3

While example

x = 4

Output: 1 2 3

Page 20: Init() Lesson 3

While example

x = 4

Output: 1 2 3

Page 21: Init() Lesson 3

While example

x = 4

Output: 1 2 3

Page 22: Init() Lesson 3

A more fun example

Let's go to koding.com and run it to see what it does!

Page 23: Init() Lesson 3

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.

Page 24: Init() Lesson 3

Infinite Loop OutputAHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH

...

Page 25: Init() Lesson 3

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

Page 26: Init() Lesson 3

Until example

x = 3

Output:

Page 27: Init() Lesson 3

Until example

x = 3

Output:

Page 28: Init() Lesson 3

Until example

x = 3

Output: 3

Page 29: Init() Lesson 3

Until example

x = 2

Output: 3

Page 30: Init() Lesson 3

Until example

x = 2

Output: 3

Page 31: Init() Lesson 3

Until example

x = 2

Output: 3 2

Page 32: Init() Lesson 3

Until example

x = 1

Output: 3 2

Page 33: Init() Lesson 3

Until example

x = 1

Output: 3 2

Page 34: Init() Lesson 3

Until example

x = 1

Output: 3 2 1

Page 35: Init() Lesson 3

Until example

x = 0

Output: 3 2 1

Page 36: Init() Lesson 3

Until example

x = 0

Output: 3 2 1

Page 37: Init() Lesson 3

Until example

x = 0

Output: 3 2 1

Page 38: Init() Lesson 3

What will this print out?

Page 39: Init() Lesson 3

Answer

0 1 2 3 4

Page 40: Init() Lesson 3

A Brief SidenoteYou might have seen that in loops I was using += and -=

These are just shorthand.

Page 41: Init() Lesson 3

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

Page 42: Init() Lesson 3

For example

x = 1

Output:

Page 43: Init() Lesson 3

For example

x = 1

Output: 1

Page 44: Init() Lesson 3

For example

x = 2

Output: 1

Page 45: Init() Lesson 3

For example

x = 2

Output: 1 2

Page 46: Init() Lesson 3

For example

x = 3

Output: 1 2

Page 47: Init() Lesson 3

For example

x = 3

Output: 1 2 3

Page 48: Init() Lesson 3

For example

x = 4

Output: 1 2 3

Page 49: Init() Lesson 3

For example

x = 4

Output: 1 2 3

Page 50: Init() Lesson 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?

Page 51: Init() Lesson 3

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 '.'

Page 52: Init() Lesson 3

What will this print out?

Page 53: Init() Lesson 3

Answer1 2 3 4 5 6 7 8 9

This is exclusive because 10 is not printed out.

Page 54: Init() Lesson 3

What will this print out?

Page 55: Init() Lesson 3

Answer1 2 3 4 5 6 7 8 9 10

This is inclusive because 10 is printed out.

Page 56: Init() Lesson 3

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

Page 57: Init() Lesson 3

What will this print out?

Page 58: Init() Lesson 3

Answer

1 3 5 7 9

This loop skipped if x was even and printed out x if it was odd.

Page 59: Init() Lesson 3

Any questions on loops?

Page 60: Init() Lesson 3

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!

Page 61: Init() Lesson 3

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.

Page 62: Init() Lesson 3

Array indicesWhat if we have the array {15, 27, 21, 13, 44, 17}?

my_array = [15, 27, 21, 13, 44, 17]

Page 63: Init() Lesson 3

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]'

Page 64: Init() Lesson 3

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'

Page 65: Init() Lesson 3

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!

Page 66: Init() Lesson 3

Sort example

But wait! These are numbers! What if we want to sort an array of strings?

Page 67: Init() Lesson 3

Sorting String Arrays

Calling '.sort!' on an array of strings will also work! The array will be sorted alphabetically.

Page 68: Init() Lesson 3

What we did today• While Loops (Be careful about infinite loops!)

• Until Loops

• For Loops

• Next

• Arrays

• Simple Sorting

Page 69: Init() Lesson 3

Any questions before we get to the homework?

Page 70: Init() Lesson 3

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!

Page 71: Init() Lesson 3