Flow control Flashcards
What do “puts” and “gets” mean?
put string and get string (both output a string, so if I need something else, set it up) e.g. a = gets.chomp.to_i #if I want an integer output
How do you make a one line “if” statement?
e.g.
if x == 3 then puts “x is 3” end
How do you use “if” statement in the end and how can you turn it into an “unless” statement?
e.g.
puts “x is 3” if x == 3
puts “x is NOT 3” unless x == 3
List the comparison operators!
< - less than > - greater than <= - less than or equal >= - greater than or equal == - equal to != - not equal to && - and operator || - or operator ! - not operator
What is the order of precedence in Ruby? (deciding how to evaluate multiple expressions)
- <=, , >= comparison
- ==, != equality
- && logical AND
- || logical OR
What is a ternary operator? (with example)
One line if/else statement, it uses the combination of ? and :
e.g.
3 < 4 ? “this is true” : “this is false”
(it seems “puts” is not needed)
What are the keywords used in
a “case” statement?
Case, when, else, end
Use “case” statement to break down what will I see if I go travelling this year!
puts “Where are you going this year?”
travel = gets.chomp
country = case travel when "australia" "See the opera house and koalas" when "new_zealand" "check the Shire and beautiful nature" when "singapore" "check the city and beaches" when "hungary" "spend time with family and friends" else "I'll be in Japan" end
puts country
How are expressions evaluated in flow control in Ruby?
Every expression evaluates "true" except for "false" and "nil" e.g. we can even say if x = 5 puts "how can this be true?" else puts "it is not true" end
(Since it’s assigning the variable x the value of 5, it will always evaluate to “true”.
What is the standard format when naming variables in for loops?
To make clear what we are iterating over.
e.g.
friends = [“Joe”, “Ted”, “Todd”, “Mike”]
for friend in friends
puts “Hello #{friend}!”
end
Hello Joe!
Hello Ted!
Hello Todd!
Hello Mike!
Write a loop that generates a random number between 0 and 50 and stop if the number is between 20 and 25!
loop do number = rand(51) puts number break if number.between(20, 25) end
What are statement modifiers?
Appending if or unless condition (others too) to form shorter, more readable code.
When added to the end of a statement, are called modifiers.
e.g.
sun = [“visible”, “hidden”].sample
puts “The sun is so bright!” if sun == “visible”
puts “The clouds are blocking the sky!” unless sun == “visible”
Rewrite the following case statement in 5 lines!
e.g.
rock_paper_scissors = [“rock”, “paper”, “scissors”]
case rock_paper_scissors when "rock" puts "Fist" when "paper" puts "Palm" else puts "fingers" end
rock_paper_scissors = [“rock”, “paper”, “scissors”]
case rock_paper_scissors when "rock" then puts "Fist" when "paper" then puts "Palm" else puts "Fingers" end
How to print the result with a ternary operator?
Put the code between parentheses.
count = 1
loop do count.odd? ? (puts "#{count} is odd!") : (puts "#{count} is even!") count += 1 break if count > 5 end
=> 1 is odd! 2 is even! 3 is odd! 4 is even! 5 is odd!