Logic & Conditionals Flashcards

1
Q

dog = “satisfied”

if dog == "hungry"
  puts "Refilling food bowl."
else
  puts "Reading newspaper."
end

What will happen?

A

puts “Reading newspaper.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

dog = “thirsty”

if dog == "hungry"
  puts "Refilling food bowl."
elsif dog == "thirsty"
  puts "Refilling water bowl."
else
  puts "Reading newspaper."
end

What will happen?

A

puts “Refilling water bowl.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

dog = “cuddly”

if dog == "hungry"
  puts "Refilling food bowl."
elsif dog == "thirsty"
  puts "Refilling water bowl."
elsif dog == "playful"
  puts "Playing tug-of-war."
elsif dog == "cuddly"
  puts "Snuggling."
else
  puts "Reading newspaper."
end

What will happen?

A

puts “Snuggling.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we write the following as a ternary operator?

age = 1
if age < 2
  "baby"
else
  "not a baby"
end
A

age = 1

age < 2 ? “baby” : “not a baby”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the basic structure of a ternary operator?

A

conditional ? action_if_true : action_if_false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What would an if statement modifier look like?

A

this_year = 2020

puts “Hey, it’s 2016!” if this_year == 2016

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What would an unless statement modifier look like?

A

this_year = 2020

puts “Hey, it’s not 2016!” unless this_year == 2016

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: The following if statement would be a good candidate for using a ternary operator instead.

if condition_a
  something
else
  something_else
end
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we turn the following if statement into a ternary operator?

if condition_a
  something
else
  something_else
end
A

condition_a ? something : something_else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will happen in the following code?

name = “Steven”
puts “Hi, #{name}” if name == “Steven”

A

puts “Hi, Steven”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will happen in the following code?

name = “Steven”
puts “Hi, #{name}” unless name == “Steven”

A

Nothing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When is it good to use a case statement instead of an if statement?

A

It’s good to use a case statement when you have a lot of if and elsif statements to check the value of one variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would the following if/elsif statement look like as a case statement?

name = “Alice”

if name == "Alice"
  puts "Hello, Alice!"
elsif name == "The White Rabbit"
  puts "Don't be late, White Rabbit"
elsif name == "The Mad Hatter"
  puts "Welcome to the tea party, Mad Hatter"
elsif name == "The Queen of Hearts"
  puts "Please don't chop off my head!"
else
  puts "Whoooo are you?"
end
A

case name

  when "Alice"
    puts "Hello, Alice!"
  when "The White Rabbit"
    puts "Don't be late, White Rabbit"
  when "The Mad Hatter"
    puts "Welcome to the tea party, Mad Hatter"
  when "The Queen of Hearts"
    puts "Please don't chop off my head!"
  else 
    puts "Whoooo are you?"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will happen in the following code?

greeting = “friendly_greeting”

case greeting
  when "unfriendly_greeting"
    puts "What do you want!?"
  when "friendly_greeting"
    puts "Hi! How are you?"
end
A

puts “Hi! How are you?”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will happen in the following code?

current_weather = “raining”

case current_weather
  when "sunny"
    puts "grab some sunscreen!"
  when "raining"
    puts "grab an umbrella"
  when "snowing"
    puts "bundle up"
end
A

puts “grab an umbrella”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will happen in the following code?

grade = “A”

case grade
  when "A"
    puts "Good job, Homestar!"
  when "B"
    puts "You can totally do better!"
  when "C"
    puts "Find a mentor to help you!"
  else
    puts "You're just making that up!"
end
A

puts “Good job, Homestar!”

17
Q

How can we make the following code shorter using the || operator?

if letter == "a"
  true
elsif letter == "e"
  true
elsif letter == "i"
  true
elsif letter == "o"
  true
elsif letter == "u"
  true
else
  false
end
A
if letter == "a" || letter == "e" || letter ==  "i" || letter == "o" || letter == "u"
  true
else
  false
end
18
Q

What will the following method call return?

def vowels_with_if_single_line(letter)
  true if letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u"
end

vowels_with_if_single_line(“r”)

A

False