Flow control Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What do “puts” and “gets” mean?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you make a one line “if” statement?

A

e.g.

if x == 3 then puts “x is 3” end

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

How do you use “if” statement in the end and how can you turn it into an “unless” statement?

A

e.g.
puts “x is 3” if x == 3

puts “x is NOT 3” unless x == 3

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

List the comparison operators!

A
< - less than
> - greater than
<= - less than or equal
>= - greater than or equal
== - equal to
!= - not equal to
&amp;&amp; - and operator
|| - or operator
! - not operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the order of precedence in Ruby? (deciding how to evaluate multiple expressions)

A
  1. <=, , >= comparison
  2. ==, != equality
  3. && logical AND
  4. || logical OR
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a ternary operator? (with example)

A

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)

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

What are the keywords used in

a “case” statement?

A

Case, when, else, end

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

Use “case” statement to break down what will I see if I go travelling this year!

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are expressions evaluated in flow control in Ruby?

A
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”.

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

What is the standard format when naming variables in for loops?

A

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!

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

Write a loop that generates a random number between 0 and 50 and stop if the number is between 20 and 25!

A
loop do 
number = rand(51)
puts number
break if number.between(20, 25)
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are statement modifiers?

A

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”

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

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
A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to print the result with a ternary operator?

A

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!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly