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 input 
First, 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 values 
Now 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' 
optional 'else' 
'end' to end decision 
We'll go into more detail 
on the next few slides
if 
An 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?
else 
An 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?
elsif 
elsif 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?
unless 
An 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'
and 
The 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!"
or 
The 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!"
not 
The 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.

More Related Content

PDF
ACM init() Spring 2015 Day 1
KEY
Thinking Like a Programmer
PDF
Effective text editing with vim
PDF
Cis 1403 lab5_loops
PDF
Cis 1403 lab1- the process of programming
PPTX
Apex for humans
ACM init() Spring 2015 Day 1
Thinking Like a Programmer
Effective text editing with vim
Cis 1403 lab5_loops
Cis 1403 lab1- the process of programming
Apex for humans

Similar to Init() Lesson 2 (20)

PPTX
Data Science-2.pptx for engineering students
PPT
Introduction to Python Lesson Three_041313.ppt
PDF
An SEO’s Intro to Web Dev PHP
PDF
CPAP.com Introduction to Coding: Part 1
PPT
Learn python
PDF
Vpet sd-1.25.18
PDF
PDF
powerpoint 1-19.pdf
PDF
Bavpwjs1113
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PDF
Build a virtual pet with javascript (may 2017)
PDF
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
PPTX
Dasar-Dasar Program Keahlian Program Perangakat Lunak dan Gim
PPTX
Writing testable code
PPTX
Brixton Library Technology Initiative Week1 Recap
PDF
Build a virtual pet with javascript (april 2017)
PDF
Basics of Programming - A Review Guide
PPTX
powerpoint 2-7.pptx
PDF
Test-Driven Development
Data Science-2.pptx for engineering students
Introduction to Python Lesson Three_041313.ppt
An SEO’s Intro to Web Dev PHP
CPAP.com Introduction to Coding: Part 1
Learn python
Vpet sd-1.25.18
powerpoint 1-19.pdf
Bavpwjs1113
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
Build a virtual pet with javascript (may 2017)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Dasar-Dasar Program Keahlian Program Perangakat Lunak dan Gim
Writing testable code
Brixton Library Technology Initiative Week1 Recap
Build a virtual pet with javascript (april 2017)
Basics of Programming - A Review Guide
powerpoint 2-7.pptx
Test-Driven Development
Ad

More from UCLA Association of Computing Machinery (13)

PDF
UCLA ACM Spring 2015 general meeting
PDF
UCLA Geek Week - Claim Your Domain
PDF
Intro to Hackathons (Winter 2015)
PDF
An Introduction to Sensible Typography
PPTX
Building a Reddit Clone from the Ground Up
PDF
ACM Fall General Meeting
PDF
ACM Teach - Hackathon Tips and Tricks - Spring 2014
PDF
ACM General Meeting - Spring 2014
UCLA ACM Spring 2015 general meeting
UCLA Geek Week - Claim Your Domain
Intro to Hackathons (Winter 2015)
An Introduction to Sensible Typography
Building a Reddit Clone from the Ground Up
ACM Fall General Meeting
ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM General Meeting - Spring 2014
Ad

Recently uploaded (20)

PPTX
Education and Perspectives of Education.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
semiconductor packaging in vlsi design fab
PDF
HVAC Specification 2024 according to central public works department
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Education and Perspectives of Education.pptx
My India Quiz Book_20210205121199924.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
A powerpoint presentation on the Revised K-10 Science Shaping Paper
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
Introduction to pro and eukaryotes and differences.pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Journal of Dental Science - UDMY (2022).pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Literature_Review_methods_ BRACU_MKT426 course material
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
semiconductor packaging in vlsi design fab
HVAC Specification 2024 according to central public works department
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Complications of Minimal Access-Surgery.pdf
Computer Architecture Input Output Memory.pptx
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf

Init() Lesson 2

  • 1. ACM init() Day 2: October 29, 2014 Happy Birthday Internet!
  • 2. 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
  • 5. No code. This is your homework.
  • 6. Interacting with the user • Programs can change based on user input • We want a way to store and use user-inputted values
  • 7. 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
  • 8. Getting user input First, 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'
  • 9. Using user-inputed values Now 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.
  • 10. Let's look at the full program:
  • 11. Any questions on user input? Hint: You now know enough to write a Mad Libs program.
  • 12. 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'
  • 13. Tells Ruby input should be an integer 'if' to start the decision optional one or more 'elsif' optional 'else' 'end' to end decision We'll go into more detail on the next few slides
  • 14. if An 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.
  • 15. if example What will this print out?
  • 16. else An 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.
  • 17. else example What will this print out?
  • 18. elsif elsif 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.
  • 19. elsif example If n = 5 and m = 1, what will this print out?
  • 20. unless An 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
  • 21. unless example What will this print out? What type of variable is 'studied'?
  • 22. What will this print out?
  • 23. Answer "The string length is 9!"
  • 24. What will this print out?
  • 25. Answer "The string length is 7 or less!"
  • 26. What will this print out?
  • 27. Answer "The string length is 10 or greater!"
  • 28. What will this print out?
  • 29. Trick Question! There was no 'end' after the unless-else block. This will cause an error. Now what will this print out?
  • 31. Any questions on if, else, elsif, or unless?
  • 32. 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.
  • 33. 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.
  • 34. Greater/Less Than... • We already saw an example of less than and greater than in previous slides • Less than: < • Greater than: >
  • 35. ... 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: >=
  • 36. 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'
  • 37. and The 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.
  • 39. What will this print out?
  • 40. Answer "The boolean is true!"
  • 41. or The 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.
  • 43. What will this print out?
  • 44. Answer "The or was true!"
  • 45. not The symbol for 'not' is ! ! ! makes true values false and false values true Let's look at some examples.
  • 47. What will this print out?
  • 48. Answer "Two is less than three."
  • 49. 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.
  • 56. What we did today • Getting user input • Control flow: if, else, elsif, unless • Making comparisons: ==, !=, <, >, <=, >= • Boolean operators: &&, ||, !
  • 57. Any questions before we move on to homework?
  • 58. 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.