Ruby Flow Control Flashcards
conditional flow
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.
conditional
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
unless
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
boolean value
A boolean value is either true or false, nothing else.
comparison operators
(, <=, >=, ==, !=, &&, ||)
comparison operators always return a boolean value.
< - 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
>
>
- 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
<=
<= - 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
> =
> = - 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
==
== - 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
!=
!= - 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
&&
&& - 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
||
|| - 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
!
! - 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
order of precedence
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.