Ruby Flow Control Flashcards

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

conditional flow

A

When you are writing programs, you want your data to make the right decisions. You want your data to do the right thing when it’s supposed to.

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

conditional

A

A conditional is a fork (or many forks) in the road. Your data approaches a conditional and the conditional then tells the data where to go based on some defined parameters.

using a combination of if statements and comparison operators (, <=, >=, ==, !=, &&, ||)

basic logical structures that are defined with the reserved words if, else, elsif, and end.

conditional.rb

puts “Put in a number”
a = gets.chomp.to_i

if a == 3
  puts "a is 3"
elsif a == 4
  puts "a is 4"
else
  puts "a is neither 3, nor 4"
end
# Example 4: must use "then" keyword when using 1-line syntax
if x == 3 then puts "x is 3" end

puts “x is 3” if x == 3

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

unless

A

Ruby also has a reserved word, unless. It acts as the opposite of if, so you can use it like this:

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

boolean value

A

A boolean value is either true or false, nothing else.

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

comparison operators

A

(, <=, >=, ==, !=, &&, ||)

comparison operators always return a boolean value.

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

< - The “less than” symbol. Anything to the left of the symbol has a lower value than anything to the right of the symbol.

irb :001 > 4 < 5
=> true

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

>

A

>

  • The “greater than” symbol. Anything to the left of the symbol has a higher value than anything to the right of the symbol.

irb :002 > 4 > 5
=> false

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

<=

A

<= - The “less than or equal to” symbol. Anything to the left of the symbol is less than or equal to anything on the right.

irb :001 > 4 <= 5
=> true

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

> =

A

> = - the “greater than or equal to” symbol. Anything to the left of the symbol is greater than or equal to anything on the right.

irb :002 > 5 >= 5
=> true

irb :003 > 4 >= 5
=> false

irb :004 > 4 >= 3
=> true

irb :005 > 4 >= 4
=> true

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

==

A

== - The “is equal to” operator. Anything to the left of the symbol is exactly equal to anything on the right.

irb :001 > 5 == 5
=> true

irb :002 > 5 == 6
=> false

irb :003 > ‘5’ == 5
=> false

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

!=

A

!= - The “not equal to” operator. Anything to the left of the symbol is not equal to anything to the right.

irb :001 > 4 != 5
=> true

irb :002 > 4 != 4
=> false

irb :003 > 4 != 156
=> true

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

&&

A

&& - the “and” operator. Expressions to the left and to the right of this operator have to be both true for the entire expression to be evaluated to true.

irb :001 > (4 == 4) && (5 == 5)
=> true

irb :002 > (4 == 5) && (5 == 5)
=> false

irb :002 > (4 == 5) && (5 == 6)
=> false

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

||

A

|| - the “or” operator. Either the expression to the left has to be true, or the expression to the right has to be true for the entire expression to be evaluated to true.

irb :001 > (4 == 4) || (5 == 5)
=> true

irb :002 > (4 == 5) || (5 == 5)
=> true

irb :002 > (4 == 5) || (5 == 6)
=> false

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

!

A

! - the “not” operator. When you add this in front of a boolean expression it will change that boolean value to its opposite.

irb :001 > !(4 == 4)
=> false

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

order of precedence

A

The following is a list of operations from highest order of precedence (top) to lowest (bottom).

<=, , >= - Comparison
==, != - Equality
&& - Logical AND
|| - Logical OR

if x && y || z
# do something
end

First the x && y statement will be executed. If that statement is true, then the program will execute the # do something code on the next line. If the x && y statement is false, then the z will be evaluated. If the z is true, the code on the next line will be evaluated. If the z is false, then the code will exit the if statement.

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

ternary operator

A

Ruby has a nice option for short and concise conditional if statements. The ternary operator is a common Ruby idiom that makes a quick if/else statement easy and keeps it all on one line.

The ternary operator uses a combination of the ? and :.

irb :001 > true ? “this is true” : “this is not true”
=> “this is true”

irb :001 > false ? “this is true” : “this is not true”
=> “this is not true”

17
Q

case statement

A

A case statement has similar functionality to an if statement but with a slightly different interface.

Case statements use the reserved words case, when, else, and end.

a = 5

case a
when 5
  puts "a is 5"
when 6
  puts "a is 6"
else
  puts "a is neither 5, nor 6"
end

You can also save the result of a case statement into a variable.

a = 5

answer = case a
  when 5
    "a is 5"
  when 6
    "a is 6"
  else
    "a is neither 5, nor 6"
  end

puts answer

You don’t necessarily have to give case an argument either.

a = 5

answer = case
  when a == 5
    "a is 5"
  when a == 6
    "a is 6"
  else
    "a is neither 5, nor 6"
  end

puts answer