In this section, we will learn what the if-else statements are and how to use them in Ruby.
Ruby if else Statement (AKA if-then Statement)
The if-else statements in Ruby are used to create conditional statements. Basically using these statements, we can tell our program to run a set of instructions if a certain condition is met, otherwise if those conditions were false, then skip part of the instructions and run another task.
Ruby if-else Syntax:
This is the syntax of if else statement:
if condition #instructions to run else #instructions to run end
- `if`: In order to create an if statement, we start with the keyword if.
- `condition`: this is where we define a condition that will be evaluated and if and only if the result was true, the body of the if statement (which can contain instructions) will be executed.
- `else`: the else statement represents another block of code that will be executed only if the result of the condition in the if statement was false!
- `end`: the end keyword represents the end of the if else statement.
Note that the use of `else` statement is optional and can be ignored. In that case, the syntax would be like:
if condition #instructions to run…. end
Also remember that only if or else statement will run but not both! Basically, if the result of an if statement was true, then its body gets executed and the else statement will be ignored. But if the condition resulted false, then the if statement will be skipped and its else statement runs instead.
Example: using if else in Ruby
age = 23 if age > 21 puts "You can go in" else puts "Sorry you can't go in " end first_name = "John" if first_name == "Omid" puts "Your name is Omid" else puts "Your name is not Omid" end
Output:
You can go in Your name is not Omid
How Does if-else Statement Work in Ruby?
In the example above, we have two if statements:
In the first if statement, the condition is to check if the `age` variable has a value higher than 21. Now, because the value of this variable is higher than 21, then the condition of the if statement is true and so its body will run.
Note that because the body of the if statement ran, the else statement is skipped then.
In the second if statement we have a condition to check and see if the value of the `first_name` variable is equal to `Omid`. But because this variable’s value is not equal to `Omid` then the result of the condition becomes false and so the body of the if statement will be skipped and instead the body of its else statement will run as a result.
Note: the use of else statement is optional and we could simply remove them from the conditions in our example.
Ruby if-elsif (AKA ladder) Statement
Sometimes we have more than one condition and want to check those conditions and run another set of instructions, if, the if statement’s condition resulted false!
This is where we can use the if-elsif or ladder statements!
The elsif statement stands for `else if` and is used along with an if statement to add more if statements with conditions and blocks of codes for those conditions.
Ruby if-elsif Syntax:
if condition #instructions... elsif condition #instructions... elsif condition #instructions... else #instructions... end
`elsif`: using the `elsif` keyword, we can add a new conditional block to the if statement and that will be checked if the `if` statement’s result was false.
Note that we can put as many `elsif` statements to an if statement as we want.
When it comes to if-elsif, your program will first check the if statement’s condition to see if that result true. If it did, then it will run the body of the if statement and ignore the result of `elsif` and the `else` statement. But if the `if` statement’s condition was false, then your program will check the first `elsif` statement condition. If that was true, then its body will run and the rest of `elsif` statements will be ignored! Otherwise, if the condition of the first `elsif` statement resulted false, the body will be skipped and the second elsif statement will be checked instead.
This process continues until one of the elsif statements result true or if none of them resulted true then the body of the else statement will run if there’s one or if there’s no else statement, then the entire `if-elsif` statements will be skipped.
Example: using Ruby if-elsif statement
puts "Please enter a name" first_name = gets.chomp if first_name == "Omid" puts "The name is Omid" elsif first_name == "Jack" puts "The name is Jack" elsif first_name == "John" puts "The name is John" else puts "The name is unknown..." end
Output:
Please enter a name Omid The name is Omid
In this example, the program is asking for an input value (a name) from a user. So we entered the value “Omid”.
Now the condition of the `if` statement is to see if the `first_name` variable’s value is equal to the value `Omid` and because this is true, then the body of the if statement ran and so the rest of `elsif` statements and the `else` statement are skipped.
Ruby else statement Notes:
- The else statement always appears as the last statement in an if statement. This means if there are multiple elsif statement in an if statement, they appear before the else statement.
- The use of else statement is optional and can be ignored.
Example: Ruby else statement
puts "Please enter a name" first_name = gets.chomp if first_name == "Omid" puts "The name is Omid" end puts "After the if statement"
Output:
Please enter a name JLO After the if statement
Note that in this example, we didn’t use the `else` statement at all. So here we have only the if statement and if the condition of this statement results true, its body will execute. But because we set the value JLO as the input for the program, this value is not equal to the value `Omid` and so the result of the condition in the if statement is false and its body is skipped then.
Ruby Nested if..else statement
The body of the if statement is the place where we can write code of any type. There’s no limitation in the body of an if statement. This includes writing another if statement within the body of an if statement. This process is known as nesting if statement.
Note: an if statement that is created within the body of another if statement is called nested if statement.
Example: creating nested if..else statement in Ruby
first_name = "John" salary = 100000 if first_name == "John" puts "The name is John" if salary > 50000 puts "The salary of John is higher than 50000" else puts "The salary of John is less than 50000" end else puts "The name is unknown" end
Output:
The name is John The salary of John is higher than 50000
Note that each if statement has its own `end` clause, as you see in the example above.
More to Read: