Ruby Intro/Flow Control Flashcards
What type of operator is this?
2 * 3 = 6 true ? puts {“Hello”) : puts (“Goodbye”)
a ternary operator. these let you make short “if” statements on one line
if the expression before ? is true, the first string is printed. if the expression is false, the second string is printed
Basically, what do conditionals do?
control the flow of data
control which path(s) the data will take in the program
“forks in the road”
1:
Which example shows the correct order?
if elsif else end ----------------- #2:
if
else
elsif
end
1 is correct
Is this formatting correct?
if a == 3
puts “a is 3”
else
puts “a is neither 3, nor 4”
No. the “puts” should be indented
When putting an “if” statement on just one line, which word must you include?
then
if x == 3 then puts “good” end
Which one is correct?
puts “good” if x == 3
if x=3 then puts “good” end
they are both possible
ruby allows both forms
!=
These are some comparison operators:
<=
>
What type of value do comparison operators always return?
a boolean value
true/false
True or false?
(3 * 6) == “eighteen”
false
the two values being compared must be of the same type
Why is this true?
irb :006 > “42” > “402”
=> true
because ruby does a character-by-character comparison (moving left to right):
4 and 4 = same
2 and 0 = not the same, and 2 is greater
(the comparison stops here)
What do these let you do?
&&
|
combine conditional expressions to make a larger expression
(3 > 2 == false) && (true == true)
false
(BOTH EXPRESSIONS TO THE LEFT AND RIGHT OF && MUST BE TRUE FOR THE ENTIRE EXPRESSION TO BE TRUE)
2 + 4 = 7 | | false == false
true
(ONLY ONE EXPRESSION NEEDS TO BE TRUE FOR THE ENTIRE EXPRESSION TO BE TRUE)