+ All Categories
Home > Education > Init() day2

Init() day2

Date post: 28-Jan-2018
Category:
Upload: kellymarieomalley
View: 152 times
Download: 0 times
Share this document with a friend
58
ACM init() Day 2: October 29, 2014 Happy Birthday Internet!
Transcript

ACM init()Day 2: October 29, 2014

Happy Birthday Internet!

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

Mad Libs Demo

Mad Libs Demo

No code. This is your homework.

Interacting with the user

• Programs can change based on user input

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

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

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'

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.

Let's look at the full program:

Any questions on user input?

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

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'

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

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.

if example

What will this print out?

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.

else example

What will this print out?

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.

elsif example

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

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

unless example

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

What will this print out?

Answer

"The string length is 9!"

What will this print out?

Answer

"The string length is 7 or less!"

What will this print out?

Answer

"The string length is 10 or greater!"

What will this print out?

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

an error.

Now what will this print out?

Answer

"Bye"

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

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.

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.

Greater/Less Than...

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

• Less than: <

• Greater than: >

... 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: >=

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'

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.

&& examples

What will this print out?

Answer

"The boolean is true!"

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.

|| examples

What will this print out?

Answer

"The or was true!"

notThe symbol for 'not' is !

!! makes true values false and false values true

Let's look at some examples.

! examples

What will this print out?

Answer

"Two is less than three."

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.

True or False?

Answer

True or False?

Answer

True or False?

Answer

What we did today

• Getting user input

• Control flow: if, else, elsif, unless

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

• Boolean operators: &&, ||, !

Any questions before we move on to homework?

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.


Recommended