- Become familiar with a if ... elsif ... else statements
- Practice effectively using logical operations as conditions
Keep in mind - although what we've covered seems very mathematically oriented, programming is really just supposed to be a way to tell a computer what you would do as a person.
For that reason, we need to discover the tools that are available to us for us to continue problem solving.
condition = true
if (condition)
puts "You will never know my secrets"
else
puts "Deepest Darkest Secret: I love Corgis"
endNote:
Talk about the syntax here. Note what has conditions and notice the
single end statement. They most likely haven't been exposed to end
until now, so go over what that means.
else also happens at the very last bit
This is an example of control flow
TODO: add a bit about the phrase control flow
In English, we naturally have conditional logic. Check out the following scenario:
"If it's past my bedtime, I'll tell my parents I'm going to sleep. If I have one hour left, I'll tell them I'm going to watch a show. Otherwise, I'm going to just tell them I love them."
if (time > bedtime)
# notice that this code is indented - that can be done using a single
# tab
puts "Parents, I'm going to sleep"
elsif (time == bedtime - 1)
puts "Parents, I'm going to watch a show"
else
puts "Parents, I love you"
endNote:
Make sure to explain the syntax for commenting in Ruby. Also note that there are multiple ways to do comments (multi-line comments) and that they differ from language to language.
-
Write a series of conditional statements that will print the statement "this number is even" if a given number is even, print the statement "this number is odd" if a given number is odd, and print "this number is neither even or odd" for all other situations.
-
Write a series of conditional statements that will print the string "fizz" if the given number is divisible by 3 or the string "buzz" if the given number is divisible by 5. Otherwise print the number.
puts("Input a number!")
number = gets.to_i
if (number > 10) && (number < 20)
puts("Your number was greater than ten AND less than twenty!")
else
puts("Your number was either less than ten, OR greater than twenty!")
endNote:
This is the first time we ask for user input. Take a moment to talk about
the gets method
I find it useful to think of gets and puts as opposites
puts("Input a number!")
number = gets.to_i
if (number == 7) || (number == 13)
puts("Your number is magic!")
else
puts("Your number is not magical")
end# Wrong!
puts("Input a number!")
number = gets.to_i
if number == (7 || 13) # MISTAKE
puts("You input a magic number!")
else
puts("Your number is not magical")
end-
Let's extend the
Fizzbuzzproblem we encountered earlier. Write a series of conditional statements that will print the string "fizz" if the given number is divisible by 3, the string "buzz" if the given number is divisible by 5, or the string "fizzbuzz" if the number is divisible by both 3 and 5. Otherwise print the number. -
Write a series of conditional statements that print "found" if the given number is between the numbers 1 - 10 or is 25. Otherwise, print "definitely not found"
- Logical Operations in Conditions
- Conditional Statements
ifelsifelse