TKVR

Chris Pine, Learn to Program Chapter 6 - Flow Control

Feb 16th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.11 KB | None | 0 0
  1. # Flow Control - Branching and Looping
  2. # Comparison Methods - giving us special objects true and false, not strings
  3. puts 1 > 2 # if one object is greater than the other
  4. puts 1 < 2
  5. puts 5 >= 5
  6. puts 5 <= 4
  7. puts 1 == 1 # "are these equal?"
  8. puts 2 != 1 # "are these different?"
  9. puts 'cat' < 'dog' # compared by dictionary a-z ordering; capitals before lowercasee. So, use downcase (or upcase or  capitalize) on both words before you try to compare them.
  10.  
  11. # Branching
  12. puts 'Hello, what\'s your name?'
  13. name = gets.chomp
  14. puts 'Hello, ' + name + '.'
  15. if name == 'Chris'
  16.   puts 'What a lovely name!'
  17. end
  18. # we would like a program to do one thing if an expression is true, and another if it is false. That's what else is for:
  19. puts 'I am a fortune-teller. Tell me your name:'
  20. name = gets.chomp
  21. if name == 'Chris'
  22.   puts 'I see great things in your future.'
  23. else
  24.   puts 'Your future is. . . Oh my! Look at the time!'
  25.   puts 'I really have to go, sorry!'
  26. end
  27.  
  28. # you can have branches which themselves have branches:
  29. puts 'Hello, and welcome to 7th grade English.'
  30. puts 'My name is Mrs. Gabbard. And your name is . . .?'
  31. name = gets.chomp
  32.  
  33. if name == name.capitalize
  34.   puts 'Please take a seat, ' + name + '.'
  35. else
  36.   puts name + '? You mean ' + name.capitalize + ', right?'
  37.   puts 'Don\'t you even know how to spell your name?'
  38.   reply = gets.chomp
  39.  
  40.   if reply.downcase == 'yes'
  41.     puts 'Hmmph! Well, sit down!'
  42.   else
  43.     puts 'GET OUT!!'
  44.   end
  45. end
  46. # NOTE: write the end at the same time I write the if - ex.
  47. # if name == name.capitalize
  48. # else
  49. # end
  50. # Then fill it in with comments:
  51. # if name == name.capitalize
  52.   #  She's civil.
  53. # else
  54.   #  She gets mad.
  55. # end
  56. # and finally replace the comments with working code. Helps keep track of where I am in the code.
  57.  
  58. # Looping - repeat certain parts of a program while a certain condition is true.
  59. command = ''
  60.  
  61. while command != 'bye'
  62.   puts command
  63.   command = gets.chomp
  64. end
  65.  
  66. puts 'Come again soon!'
  67. # What if your computer gets trapped in an infinite loop? If you think this may have happened, just hold down the Ctrl key and press C.
  68.  
  69. # Some Logic to understand - DRY RULE : DONT REAPEAT YOURSELF!!!!!
  70. # here, we repeated the line puts 'What a lovely name!'
  71. puts 'Hello, what\'s your name?'
  72. name = gets.chomp
  73. puts 'Hello, ' + name + '.'
  74. if name == 'Chris'
  75.   puts 'What a lovely name!'
  76. else
  77.   if name == 'Katy'
  78.     puts 'What a lovely name!'
  79.   end
  80. end
  81.  
  82. # does the same thing when gets chris or katy
  83. puts 'Hello, what\'s your name?'
  84. name = gets.chomp
  85. puts 'Hello, ' + name + '.'
  86. if (name == 'Chris' or name == 'Katy')
  87.   puts 'What a lovely name!'
  88. end
  89.  
  90. # logical operators = or, and, not | Use parenthesis when working with these. With or, it means 'one, or the other, or both'
  91. iAmChris = true
  92. iAmPurple = false
  93. iLikeFood = true
  94. iEatRocks = false
  95.  
  96. puts (iAmChris and iLikeFood)
  97. puts (iLikeFood and iEatRocks)
  98. puts (iAmPurple and iLikeFood)
  99. puts (iAmPurple and iEatRocks)
  100. puts
  101. puts (iAmChris or iLikeFood)
  102. puts (iLikeFood or iEatRocks)
  103. puts (iAmPurple or iLikeFood)
  104. puts (iAmPurple or iEatRocks)
  105. puts
  106. puts (not iAmPurple)
  107. puts (not iAmChris )
Add Comment
Please, Sign In to add comment