## Key Concepts

Review core concepts you need to learn to master this subject

### `elsif` Statements in Ruby

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

In Ruby, an `elsif` statement can be placed between `if` and `else` statements. It allows us to check for additional conditions.

More than one `elsif` can be placed between `if` and `else`.

### Ruby not Operator

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

The `!` (not) operator in Ruby flips a boolean value. If a value is `true` then applying `!` to the value changes it to `false` and vice versa.

### Else statement in Ruby.

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

In Ruby, an `if` statement evaluates to either `true` or `false`. The code indented after the `if` portion is executed for `true` while the code indented after the `else` portion is executed for `false`.

### Comparison operators in Ruby.

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

The following comparison or relational operators are used in Ruby to compare values.

`>` - greater than; `<` - less than; `>=` - greater than or equal to; `<=` - less than or equal to; `==` - equal to

### Or operator in Ruby.

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

The `||` (or) operator is a logical operator which returns `true` if either of the expressions on left-hand side or right-hand side is `true`.

### `if` Statement in Ruby

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" 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.

### And operator in Ruby.

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

`&&` is a logical operator in Ruby which evaluates to `true` only if both expressions on either side of `&&` evaluates to `true`.

### Unless statement in Ruby.

```print "enter a number: " num = gets.chomp num = num.to_i; if num == 5 print "number is 5" elsif num == 10 print "number is 10" elsif num == 11 print "number is 11" else print "number is something other than 5, 10, or 11" end```

An `unless` statement in Ruby is used to evaluate an expression. If the expression evaluates to `false`, then the code following `unless` is executed.

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…
1. 1
Now that we can direct our program using if / else statements, we can produce different results based on different user input. In this project, we’ll combine control flow with a few new Ruby strin…
2. 2
First, we should print a statement to prompt the user for input, then set that input to a variable using gets.chomp.
3. 3
We want to make sure we capture both “S” and “s” in the user’s input. We could write separate if / else statements to handle this, but we can also use .downcase! to convert the user’s input to all …
4. 4
All right! Time to add in a little control flow. For the if half of our branch, we want to check whether the user’s input contains an “s”. if string_to_check.include? “substring” We can do tha…
5. 5
Good! Now let’s complete our if statement. When we find “s”, we want Ruby to replace every instance of “s” it finds with “th”. We can do this with the .gsub! method, which stands for global **…
6. 6
The hard part’s over! Now we just need to let the user know if we don’t find any instances of the letter “s”.
7. 7
Home stretch—now we want to display the Daffy Duckified string to the user. You can do that using the string interpolation we learned earlier: my_string = “muchachos” print “Adios, #{my_string}!” #…
8. 8
Great work! How might you improve this project? You could: * Add an additional if statement to re-prompt the user for input if they don’t enter anything * Think about how you might account for wo…