# Flow Control - Branching and Looping # Comparison Methods - giving us special objects true and false, not strings puts 1 > 2 # if one object is greater than the other puts 1 < 2 puts 5 >= 5 puts 5 <= 4 puts 1 == 1 # "are these equal?" puts 2 != 1 # "are these different?" 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. # Branching puts 'Hello, what\'s your name?' name = gets.chomp puts 'Hello, ' + name + '.' if name == 'Chris' puts 'What a lovely name!' end # 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: puts 'I am a fortune-teller. Tell me your name:' name = gets.chomp if name == 'Chris' puts 'I see great things in your future.' else puts 'Your future is. . . Oh my! Look at the time!' puts 'I really have to go, sorry!' end # you can have branches which themselves have branches: puts 'Hello, and welcome to 7th grade English.' puts 'My name is Mrs. Gabbard. And your name is . . .?' name = gets.chomp if name == name.capitalize puts 'Please take a seat, ' + name + '.' else puts name + '? You mean ' + name.capitalize + ', right?' puts 'Don\'t you even know how to spell your name?' reply = gets.chomp if reply.downcase == 'yes' puts 'Hmmph! Well, sit down!' else puts 'GET OUT!!' end end # NOTE: write the end at the same time I write the if - ex. # if name == name.capitalize # else # end # Then fill it in with comments: # if name == name.capitalize # She's civil. # else # She gets mad. # end # and finally replace the comments with working code. Helps keep track of where I am in the code. # Looping - repeat certain parts of a program while a certain condition is true. command = '' while command != 'bye' puts command command = gets.chomp end puts 'Come again soon!' # 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. # Some Logic to understand - DRY RULE : DONT REAPEAT YOURSELF!!!!! # here, we repeated the line puts 'What a lovely name!' puts 'Hello, what\'s your name?' name = gets.chomp puts 'Hello, ' + name + '.' if name == 'Chris' puts 'What a lovely name!' else if name == 'Katy' puts 'What a lovely name!' end end # does the same thing when gets chris or katy puts 'Hello, what\'s your name?' name = gets.chomp puts 'Hello, ' + name + '.' if (name == 'Chris' or name == 'Katy') puts 'What a lovely name!' end # logical operators = or, and, not | Use parenthesis when working with these. With or, it means 'one, or the other, or both' iAmChris = true iAmPurple = false iLikeFood = true iEatRocks = false puts (iAmChris and iLikeFood) puts (iLikeFood and iEatRocks) puts (iAmPurple and iLikeFood) puts (iAmPurple and iEatRocks) puts puts (iAmChris or iLikeFood) puts (iLikeFood or iEatRocks) puts (iAmPurple or iLikeFood) puts (iAmPurple or iEatRocks) puts puts (not iAmPurple) puts (not iAmChris )