Key Concepts

Review core concepts you need to learn to master this subject

if Statement in Ruby

number = 10 if number == 10 puts "Your condition was true!" end

An if statement in Ruby evaluates an expression, which returns either true or false. If the expression is true, Ruby executes the code block that follows the if whereas if the expression is false, Ruby returns nil.

In this example, the string "Your condition was true!" will print because the condition number == 10 is true.

Control Flow in Ruby
Lesson 1 of 2
  1. 1
    You may have noticed that the kinds of programs we’ve written so far in Ruby aren’t very flexible. Sure, they can take user input, but they always produce the same result based on that input; they …
  2. 2
    Ruby’s if statement takes an expression, which is just a fancy word for something that has a value that evaluates to either true or false. If that expression is true, Ruby executes the block of…
  3. 3
    The partner to the if statement is the else statement. An if/else statement says to Ruby: “If this expression is true, run this code block; otherwise, run the code after the else statement.” Here’s…
  4. 4
    What if you want more than two options, though? It’s elsif to the rescue! The elsif statement can add any number of alternatives to an if/else statement, like so: if x y puts “x is greater than…
  5. 5
    Sometimes you want to use control flow to check if something is false, rather than if it’s true. You could reverse your if/else, but Ruby will do you one better: it will let you use an unless sta…
  6. 6
    In Ruby, we assign values to variables using =, the assignment operator. But if we’ve already used = for assignment, how do we check to see if two things are equal? Well, we use ==, which is a …
  7. 7
    We can also check to see if one value is less than, less than or equal to, greater than, or greater than or equal to another. Those operators look like this: * Less than: * Greater than or equal…
  8. 8
    Great work so far! You know what they say: practice makes perfect. Let’s try a few more comparators to make sure you’ve got the hang of this.
  9. 9
    Comparators aren’t the only operators available to you in Ruby. You can also use logical or boolean operators. Ruby has three: and (&&), or (||), and not (!). Boolean operators result in bo…
  10. 10
    Ruby also has the or operator (||). Ruby’s || is called an inclusive or because it evaluates to true when one or the other or both expressions are true. Check it out: true || true # => tr…
  11. 11
    Finally, Ruby has the boolean operator not (!). ! makes true values false, and vice-versa. !true # => false !false # => true
  12. 12
    You can combine boolean operators in your expressions. Combinations like (x && (y || w)) && z are not only legal expressions, but are extremely useful tools for your programs. These expression ma…
  13. 13
    Great work! So far you’ve learned: * How to use if, else, and elsif * How to use comparators / relational operators like ==, !=, , and >= * How to use boolean / logical operators like &&, ||, and !
  14. 14
    All right! You’re all on your lonesome. (Well, not quite. We’ll just leave this example here.) a = 10 b = 11 if a < b print “a is less than b!” elsif b < a print “b is less than a!” else p…
  15. 15
    Good! Now let’s review the unless statement. problem = false print “Good to go!” unless problem Remember, this is basically a short hand if statement. It will do whatever you ask unless the condi…
  16. 16
    Now let’s review comparators / relational operators. We’ve turned the tables a bit! Remember, comparators need to compare two values to each other to result in true or false 10 > 8 # true 8 > 10 …
  17. 17
    Home stretch! Let’s go over boolean operators. ( 1 == 1 ) && ( 2 == 2 ) # true ( 1 == 2 ) || ( 2 == 2 ) # true !( false ) # true 1. With && both comparisons on the left and right must evaluate…