variables
play

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


  1. Variables CS105 : Saelee

  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!"

  3. annoying

  4. D on’t R epeat Y ourself

  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!"

  6. Variables

  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

  8. 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 ¡+ ¡"!" Hello, Michael Pleased to meet you, Michael. Michael, welcome to CS 105! The name of your instructor is Michael Saelee. Thanks for reading, Michael!

  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!

  10. references to other things

  11. How it works...

  12. variable names = “identifiers”

  13. identifier naming rules

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

  15. Assignment operator: =

  16. General syntax: LHS = RHS

  17. Not “equals”!

  18. initialize before use

  19. associativity

  20. Left associative e.g., addition, multiplication

  21. Right associative e.g., exponentiation

  22. 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 var_a ¡= ¡apples var_b ¡= ¡oranges var_c ¡= ¡pears var_d ¡= ¡pears

  23. variable interpolation

  24. Syntax: "#{var_name}"

  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}!"

  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"

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend