Updated April 1, 2023
Definition of Ruby if else
If else in Ruby is a conditional statement which used to execute code on the basis of the success of one condition and failing of the given condition inside the if block will lead to the execution of the else block (here the success of the condition means the output of the condition should be true and false if the output of the condition is false), with the help of if else statement we can deal with a situation where we need to execute either code A or to the code B (if the conditional expression output for the if block is true execute the code block A and if the conditional expression of if the statement is false execute the code B block).
Syntax:
Below is the syntax for the if else in the Ruby, we can explain the below syntax in the following steps.
- On the first line of the code we have written if and passed some conditional expression, here conditional expression output will be any boolean true or false like (if a>1 here a=2) then the output will be true and (if a<1 here a=3) this will be false.
- In case if the conditional expression is failed, else block will be executed.
- Here else block is the default block which will execute
Please see the below syntax for better understanding.
if conditional expression
# This code will be executed if conditional expression1 is true
else
# This code will be executed if conditional expression1 and conditional expression2 both are false .
end
Flowchart of Ruby if else
- On the first line of the code it will start with the if block condition expression, and we can see the diagram as we have passed some conditional expression, here conditional expression output will be any boolean true or false like (if a>1 here a=2) then output will be true and (if a<1 here a=3) this will be false.
- In case the conditional expression is false in that case it will execute the else block , which is default as we have already mentioned.
- In case if the conditional expression inside the if statement is true or successful, in that case the code block will be executed.
- We can analyse it with the help of the below diagram, with the help of the if else we are able to check two conditional along with executing two code blocks on the basis of success or failure of the one condition.
Please follow the below flowchart of the if else in Ruby.
How if else Statement Works in Ruby?
Working of the if else can be explained in the following steps.
- Every if else block contains few conditional expression. conditional expression can be combination of the multiple conditions like (a==1 && b==2) so here we have used two conditions in one conditional expression, but their output will single boolean value (true or false)
- Execution of any code block always depends on the success of the conditional statement, so if we have 2 different code blocks and we want to execute them on the basis of the different situations, then if else various conditions will play an important role here.
- The else block statement is the default one, if the conditions inside the if will be successful it will execute the if code block and if the conditional expression inside the if statement gets failed in that case else block will execute.
Examples to Implement if else Statement in Ruby
Following are the examples of ruby if else are given below:
Example #1
In the below example we are checking the marks of students and if the student passes or fails in the exam, if he/she passes the exam greeting for pass will be shown and if the student is fail in the exam than greeting for fail will be shown. We can explain the below example with the help of the following steps.
- First, we defined a variable with name marks and assigned some marks like 303.
- Total marks of the examination is 600 and between, and if the marks obtained by the student is below 300 he will be considered as fail.
- In the “if statement ” we are checking the marked variable value, if it is more than 300 it will display a greeting for pass, and if the marks is less than 300 the output for file will be displayed.
- We have taken marks as the 303 which is greater than the 300 and greeting for the pass will be shown.
Please follow the below example along with the screen of output.
Code:
#An exam with maximum marks of 600 , checking for the particular marks status
marks = 303
if marks > 300
puts "The student is passed this exam as he/she score more than 300 marks"
else
puts "The student marks is very less to pass the exam it is below 300 ."
end
Output:
Example #2
Here in this example, we are writing a program to find the total number of students, if the number of students is 1 ,if the number students are more than 1 then we will consider this as the students groups.We can explain the below example in the following steps.
- First we defined a variable students, which holds the multiple students names.
- Next “if block” will start where we have given a condition which is checking the length of the students array. If the length of the student array is equal to 1 then the if code block will execute.And if the length of the array is more than 1 it will execute the else block.
Code:
#Checking of the number of students inside any student group
#Defining the student variable here , which contains the group of multiple students .
students = ['ranjan','ajay','vijay','sujit','akash']
#Here we are checking the length of array
if students.size ==1
Puts "This is Single student group as there is only one student"
else
puts "There is #{students.size} student in this student group"
end
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of if else in Ruby with the help of some useful examples, we also learned the working of if else with the help of syntax,here we have given little focus on the uses of if else in real world cases.
Recommended Articles
We hope that this EDUCBA information on “Ruby if else” was beneficial to you. You can view EDUCBA’s recommended articles for more information.