Variables CS105 : Saelee puts "Hello, Michael" puts - - PowerPoint PPT Presentation

variables
SMART_READER_LITE
LIVE PREVIEW

Variables CS105 : Saelee puts "Hello, Michael" puts - - PowerPoint PPT Presentation

Variables CS105 : Saelee puts "Hello, Michael" puts "Pleased to meet you, Michael." puts "Michael, welcome to CS 105!" puts "The name of your instructor is Michael


slide-1
SLIDE 1

CS105 : Saelee

Variables

slide-2
SLIDE 2

puts ¡"Hello, ¡Michael" puts ¡"Pleased ¡to ¡meet ¡you, ¡Michael." puts ¡"Michael, ¡welcome ¡to ¡CS ¡105!" puts ¡"The ¡name ¡of ¡your ¡instructor ¡is ¡Michael ¡Saelee." puts ¡"Thanks ¡for ¡reading, ¡Michael!"

slide-3
SLIDE 3

annoying

slide-4
SLIDE 4

Don’t Repeat Yourself

slide-5
SLIDE 5

puts ¡"Hello, ¡Michael" puts ¡"Pleased ¡to ¡meet ¡you, ¡Michael." puts ¡"Michael, ¡welcome ¡to ¡CS ¡105!" puts ¡"The ¡name ¡of ¡your ¡instructor ¡is ¡Michael ¡Saelee." puts ¡"Thanks ¡for ¡reading, ¡Michael!"

slide-6
SLIDE 6

Variables

slide-7
SLIDE 7

animal ¡ ¡ ¡ ¡= ¡"Poodle" class_id ¡ ¡= ¡105 month ¡ ¡ ¡ ¡ ¡= ¡"September" body_temp ¡= ¡98.6 puts ¡animal puts ¡class_id puts ¡month puts ¡body_temp Poodle 105 September 98.6

slide-8
SLIDE 8

Hello, Michael Pleased to meet you, Michael. Michael, welcome to CS 105! The name of your instructor is Michael Saelee. Thanks for reading, Michael! name ¡= ¡"Michael" puts ¡"Hello, ¡" ¡+ ¡name puts ¡"Pleased ¡to ¡meet ¡you, ¡" ¡+ ¡name ¡+ ¡"." puts ¡name ¡+ ¡", ¡welcome ¡to ¡CS ¡105!" puts ¡"The ¡name ¡of ¡your ¡instructor ¡is ¡Michael ¡Saelee." puts ¡"Thanks ¡for ¡reading, ¡" ¡+ ¡name ¡+ ¡"!"

slide-9
SLIDE 9

name ¡= ¡"Jane" puts ¡"Hello, ¡" ¡+ ¡name puts ¡"Pleased ¡to ¡meet ¡you, ¡" ¡+ ¡name ¡+ ¡"." puts ¡name ¡+ ¡", ¡welcome ¡to ¡CS ¡105!" puts ¡"The ¡name ¡of ¡your ¡instructor ¡is ¡Michael ¡Saelee." puts ¡"Thanks ¡for ¡reading, ¡" ¡+ ¡name ¡+ ¡"!" Hello, Jane Pleased to meet you, Jane. Jane, welcome to CS 105! The name of your instructor is Michael Saelee. Thanks for reading, Jane!

slide-10
SLIDE 10

references to other things

slide-11
SLIDE 11

How it works...

slide-12
SLIDE 12

variable names = “identifiers”

slide-13
SLIDE 13

identifier naming rules

slide-14
SLIDE 14

name curr_temp a_b_c dayOfWeek day_of_week fall07gpa i_001 105 1_var my name grades! k-9 #aaac "word"

Valid Invalid

slide-15
SLIDE 15

Assignment operator: =

slide-16
SLIDE 16

General syntax: LHS = RHS

slide-17
SLIDE 17

Not “equals”!

slide-18
SLIDE 18

initialize before use

slide-19
SLIDE 19

associativity

slide-20
SLIDE 20

Left associative e.g., addition, multiplication

slide-21
SLIDE 21

Right associative e.g., exponentiation

slide-22
SLIDE 22

var_a ¡= ¡apples var_b ¡= ¡oranges var_c ¡= ¡pears var_d ¡= ¡pears var_a ¡= ¡"apples" var_b ¡= ¡"oranges" var_c ¡= ¡var_a var_d ¡= ¡var_c ¡= ¡"pears" puts ¡"var_a ¡= ¡" ¡+ ¡var_a puts ¡"var_b ¡= ¡" ¡+ ¡var_b puts ¡"var_c ¡= ¡" ¡+ ¡var_c puts ¡"var_d ¡= ¡" ¡+ ¡var_d

slide-23
SLIDE 23

variable interpolation

slide-24
SLIDE 24

Syntax: "#{var_name}"

slide-25
SLIDE 25

name ¡= ¡"Michael" puts ¡"Hello, ¡#{name}" puts ¡"Pleased ¡to ¡meet ¡you, ¡#{name}." puts ¡"#{name}, ¡welcome ¡to ¡CS ¡105!" puts ¡"The ¡name ¡of ¡your ¡instructor ¡is ¡Michael ¡Saelee." puts ¡"Thanks ¡for ¡reading, ¡#{name}!"

slide-26
SLIDE 26

years ¡= ¡29 ¡ ¡ ¡# ¡age ¡in ¡years ¡... months ¡= ¡11 ¡ ¡# ¡... ¡and ¡months # ¡compute ¡my ¡age ¡in ¡days ¡and ¡hours days ¡= ¡years ¡* ¡365 ¡+ ¡months ¡* ¡30 ¡ ¡# ¡365 ¡days ¡in ¡a ¡year, ¡30 ¡days ¡in ¡a ¡month hours ¡= ¡days ¡* ¡24 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡24 ¡hours ¡per ¡day # ¡print ¡out ¡my ¡age ¡in ¡years, ¡days, ¡and ¡hours puts ¡"Age: ¡#{years} ¡years ¡or ¡#{days} ¡days ¡or ¡#{hours} ¡hours" # ¡compute ¡my ¡"mental" ¡age ¡in ¡years, ¡days, ¡and ¡hours years ¡= ¡years ¡-­‑ ¡10 days ¡= ¡years ¡* ¡365 ¡+ ¡months ¡* ¡30 hours ¡= ¡days ¡* ¡24 # ¡print ¡out ¡my ¡"mental" ¡age puts ¡"Mental ¡Age: ¡#{years} ¡years ¡or ¡#{days} ¡days ¡or ¡#{hours} ¡hours"