Top Banner
ACM init() Day 2: October 29, 2014 Happy Birthday Internet!
58

Init() day2

Jan 28, 2018

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Init() day2

ACM init()Day 2: October 29, 2014

Happy Birthday Internet!

Page 2: Init() day2

Any questions?• A little bit about Ruby

• How to use Koding

• Data types: numbers, string, and booleans

• Variables

• Math

• How to print to the screen

• String methods

• How to write good code

Page 3: Init() day2

Mad Libs Demo

Page 4: Init() day2

Mad Libs Demo

Page 5: Init() day2

No code. This is your homework.

Page 6: Init() day2

Interacting with the user

• Programs can change based on user input

• We want a way to store and use user-inputted values

Page 7: Init() day2

Interacting with the user• Sometimes we write programs

that change depending on what the user types in

• For example, the Mad Libs program produced a different story based off of the words we entered

• We want a way to store and use user-inputted values

Page 8: Init() day2

Getting user inputFirst, we need to tell the user that we expect something from

them. Prompt the user to enter their name:

Now, we need to store the name in a variable. The syntax for this is 'variable_name = gets.chomp'

Here we used gets.chomp to store the user's name in a variable called 'name'

Page 9: Init() day2

Using user-inputed valuesNow we have a variable with a name stored in it. How can we

use this? Let's print it out:

Notice how we accessed our name variable? The syntax to access a variable is "#{name}"

Here's how to print out the contents of a variable:

Don't forget the " " when accessing a variable or Ruby will think you are trying to write a comment.

Page 10: Init() day2

Let's look at the full program:

Page 11: Init() day2

Any questions on user input?

Hint: You now know enough to write a Mad Libs program.

Page 12: Init() day2

Control Flow

• Programs need to make decisions based on different situations - user input, calculations, etc.

• Four types of control flow

• 'if', 'elsif', 'else', and 'unless'

Page 13: Init() day2

Tells Ruby input should be an integer

'if' to start the decision

optional one or more 'elsif'

'end' to end decision

optional 'else'

We'll go into more detail on the next few slides

Page 14: Init() day2

ifAn if statement looks at an expression then exectutes a block of code if the

expression is true. Otherwise, the block of code is skipped.

if statement do something end

An if statement can stand alone. If the statement is not true, the rest of the code will be skipped until 'end' is reached.

Page 15: Init() day2

if example

What will this print out?

Page 16: Init() day2

elseAn else statement must always be paired with an if statement. If the if

statement is not true, the else statement will be executed.

if statement do something else do something else

Note that if the if statement is not true the else statement will always be executed. In an if-else code block one of the two cases will be true.

Page 17: Init() day2

else example

What will this print out?

Page 18: Init() day2

elsifelsif must always be paired with an if statement and en else statement.

However, you can have as many elsif statements as you want.

if statement do something elsif other statement do something else else do something else

Remember that you can have more than one elsif statement, but only one if statement and one else statement. If and elsif statements have arguments,

but else is at the bottom and is the fall-through case.

Page 19: Init() day2

elsif example

If n = 5 and m = 1, what will this print out?

Page 20: Init() day2

unlessAn unless statement is used to check if something is false. It is paired with an else statement. Unless the statement we are evaluating is true the else

statement will be executed.

unless statement do something else do something else

Page 21: Init() day2

unless example

What will this print out? What type of variable is 'studied'?

Page 22: Init() day2

What will this print out?

Page 23: Init() day2

Answer

"The string length is 9!"

Page 24: Init() day2

What will this print out?

Page 25: Init() day2

Answer

"The string length is 7 or less!"

Page 26: Init() day2

What will this print out?

Page 27: Init() day2

Answer

"The string length is 10 or greater!"

Page 28: Init() day2

What will this print out?

Page 29: Init() day2

Trick Question!There was no 'end' after the unless-else block. This will cause

an error.

Now what will this print out?

Page 30: Init() day2

Answer

"Bye"

Page 31: Init() day2

Any questions on if, else, elsif, or unless?

Page 32: Init() day2

Comparison Operators

• A lot of code we write depends on comparing two values.

• Ruby has operators that do the comparison for us.

• However, we have to be very careful to make sure we write the operators correctly or the comparison could be misinterpreted.

Page 33: Init() day2

Equal/Not Equal• While it would nice to write

x = y to check if two things are equal, we can't. Remember that we used 'x = ' to define variables. We need a different way to check if two things are equal.

• The syntax for checking if two things are equal is : 'x == y'. Notice the double equals.

• To see if two things are not equal we type: 'x != y'. The '!' means 'not'.

Note that the above example could be written much more

compactly in an if-else block.

Page 34: Init() day2

Greater/Less Than...

• We already saw an example of less than and greater than in previous slides

• Less than: <

• Greater than: >

Page 35: Init() day2

... or Equal To

• Less than or equal to and greater than or equal to are written by just adding an equals sign on to the end of the expression

• Less than or equal to: <=

• Greater than or equal to: >=

Page 36: Init() day2

Boolean Operators

• Remember the variable boolean that was either true or false?

• Ruby has operators that can determine if a statement is true or false

• These operators are 'and', 'or', and 'not'

Page 37: Init() day2

andThe symbol for 'and' is &&

!An && will only result in a true value if the statements on both

sides of the && are true.

Let's look at some examples.

Page 38: Init() day2

&& examples

Page 39: Init() day2

What will this print out?

Page 40: Init() day2

Answer

"The boolean is true!"

Page 41: Init() day2

orThe symbol for 'or' is ||

!|| evaluates to true if one or both statements on either side of

the || are true

Let's look at some examples.

Page 42: Init() day2

|| examples

Page 43: Init() day2

What will this print out?

Page 44: Init() day2

Answer

"The or was true!"

Page 45: Init() day2

notThe symbol for 'not' is !

!! makes true values false and false values true

Let's look at some examples.

Page 46: Init() day2

! examples

Page 47: Init() day2

What will this print out?

Page 48: Init() day2

Answer

"Two is less than three."

Page 49: Init() day2

Combining Operators

• We can combine &&, ||, and ! to evaluate statements.

• If the expression looks confusing, we can add parenthesis to clarify things. Expressions inside () will be evaluated first.

• Let's look at some examples.

Page 50: Init() day2

True or False?

Page 51: Init() day2

Answer

Page 52: Init() day2

True or False?

Page 53: Init() day2

Answer

Page 54: Init() day2

True or False?

Page 55: Init() day2

Answer

Page 56: Init() day2

What we did today

• Getting user input

• Control flow: if, else, elsif, unless

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

• Boolean operators: &&, ||, !

Page 57: Init() day2

Any questions before we move on to homework?

Page 58: Init() day2

Make your own Mad Libs!Your assignment is to make your own Mad Libs game. Here are the details: 1) You should be able to make the game using only what you have learned so far. 2) If you don't know what Mad Libs is or don't want to write your own here is a site with some samples: http://www.madglibs.com/ 3) This is optional, but highly recommended. The best way to learn programming is to practice. 4) If you have any questions or get stuck post on the Facebook group or email one of us. We will go over the solution next time we meet.