Table of contents
1.
Introduction 
2.
Make Decisions Using If Statements
3.
Conditions
4.
If: in Ruby
4.1.
Syntax
4.2.
Code
4.2.1.
Output
5.
if-else Statement
5.1.
Syntax
5.2.
Code
5.2.1.
Output
6.
elsif Statement
6.1.
Syntax
6.2.
Code
6.2.1.
Output
7.
Frequently Asked Questions
7.1.
In what particular problem do we use the if-else statements?
7.2.
Does every If Statement need an else?
7.3.
When can you use the if-else Statement?
7.4.
Does else if execute after if?
7.5.
What are the limitations of the if-else Statement?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

If: in Ruby

Author Aditya Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Ruby is a general-purpose, dynamic, reflective, and object-oriented programming language. Except for blocks, which procs and lambda have replaced, everything in Ruby is an object. The goal of Ruby's development was to create a user interface between human programmers and the computational machinery that underpins them.

If in Ruby

Ruby's syntax is similar to many other programming languages, including C and Java, making learning easy for Java and C programmers. Operating platforms including Windows, Mac OS X, and Linux are just a few that it can run on.

If: in Ruby has conditional structures that are widely used in contemporary languages. Here, we'll go through every conditional expression and modifier Ruby offers.

Let's talk about the decision-making in Ruby and how to do it in Ruby.

Make Decisions Using If Statements

Let us first understand what decision is. You can take action if something is true (the condition). 

The examples of the decisions are mentioned below:

  • "Start the heater if the room is too cold".
  • "Send an order for more of this product if we don't have enough in stock".
  • "Send him a thank-you present if he has been a customer for more than three years".
     

You accomplish taking decisions  in Ruby by utilizing if statements:

pen = 10
if pen < 2
  puts "No pens are left in the shop."
end
You can also try this code with Online Ruby Compiler
Run Code


Take note of the syntax. It is crucial to do it correctly.

We refer to the (pen < 2) in code part as a "situation."

For the code inside the condition to operate, this must be true.

If we read this in simple english, it will look like:

Print the "no pens left" message if the pen value is less than 2; otherwise, do nothing.

Conditions

We have used the "less than" symbol in the last example, but there are other symbols you may use with various connotations.

The table for conditions is mentioned below:

Symbol Description
> The “greater than” symbol is used to show greater value.
< The “less than” symbol is used to show lesser value.
!= The "not equals" symbol shows that two values are not equal.
== The “equals” symbol is used to show that two values are equal.
<= The “less than equals to” symbol is used to show that value is less or equal.
>= The “greater than equals to” symbol shows that value is greater or equal.

As mentioned above in the table, we denoted equality with two equal == symbols. 

If you want to determine whether two items are the same, use == rather than the single equals sign, which in Ruby stands for "assignment."

You won't get the desired results if you don't execute this right.

If: in Ruby

If statements are used in Ruby to determine if a comment or a block of information will be performed or not, i.e., if a condition is proper, a block of statements will be executed. Otherwise not. So let's see the syntax for the If statement in Ruby.

Syntax

if (condition) 

   # Statements are to be executed 

end  

The condition will be checked in, and according to it will move to another block. Let's see if statement in Ruby with thehelp a flow chart.

Ruby If FlowChart

Code

# If: in Ruby Program 1

star = 5
if star >= 3  
  puts "You are Ninja in coding"  
end  
You can also try this code with Online Ruby Compiler
Run Code


You can see in this code that the star is 5, which is greater than three. Thus it will enter the if condition and run the block statement, which prints the line "You are Ninja in coding."

Output

Output

if-else Statement

When the condition is true, the 'if' Statement is used to run a block of code; when the condition is false, the 'else' Statement is used to run the code block.

Syntax

if(condition)  

    # This section code will be executed if the condition is true  

else  

   # This code will if the condition is false  

end  

if-else Statement

Code

# If: in Ruby Program 2
 
a =  22

# if condition for
# checking whether a person can vote or not
if a >= 18
  puts "You are eligible for voting"
else
  puts "You are not eligible for voting"
end
You can also try this code with Online Ruby Compiler
Run Code


You can see that the value of an in this code is 22, which is more than 18. Thus it will enter the if block and execute the block statement that prints the phrase "You are eligible for voting".

Output

Output

elsif Statement

This is used to give alternatives to the user. From the top-down, 'if' statements are executed. The statement linked with that 'if' is performed as soon as one of the conditions controlling the 'if' is true, and the rest of the ladder is bypassed. The final line will be executed if none of the conditions are true.

Syntax

if(condition1)  

# if the condition1 is true, then the code to be executed

elsif(condition2)

# code to be executed if condition2 is true  

else(condition3)  

# code to be executed if condition3 is true  

end  

The condition will be checked in, and the according will go in the block. This will help with a flow chart.

else-if flow chart

Code

# If: in Ruby Program 3
 
marks = 42
if marks  < 50  
  puts "Student is failed"
 
elsif marks >= 50 && marks <= 60  
  puts "Student gets D grade"
 
elsif marks >= 70 && marks <= 80  
  puts "Student gets B grade"
   
elsif marks >= 80 && marks <= 90  
  puts "Student gets A grade"
   
elsif marks >= 90 && marks <= 100  
  puts "Student gets A+ grade"  
end
You can also try this code with Online Ruby Compiler
Run Code


As we can see in this piece of code, we check the student's grade deepening on the marks the student has gained for every 10 marks key there is elsif to check for that particular condition.

Output

Output

Frequently Asked Questions

In what particular problem do we use the if-else statements?

When a decision is made, a set of instructions is carried out using the if-else statement. Consider a real-life scenario in which you must decide based on a condition.

Does every If Statement need an else?

If any of the conditions in the parenthesis are true, the following block of code is executed. If all you want is for the code to run when the statement returns true, no else statement is required (and do nothing else if false).

When can you use the if-else Statement?

If a particular condition is true, use if to specify a block of code to be run. If the same condition is false, use otherwise to specify a code block to run. If the first condition is false, use else if to define a new condition to test.

Does else if execute after if?

If the preceding if and any preceding else if statements are evaluated to false, and the current elsif expression is evaluated to true, the else if statement is performed.

What are the limitations of the if-else Statement?

Utilizing if-else statements increases the number of code paths that must be examined. The Switch case statement is used when a program has many if statements because doing so can make the code difficult to read and complex.

Conclusion

In this article, we have extensively studied If statements and else if statements. We have discussed every concept in detail with the help of a syntax flow chart and sample code. 

After reading about the If statement in Ruby, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. See Introduction to ruby on railsDirectory Structure in RubyRuby on RailsRuby vs Python

Please check other blogs on Ruby case statements such as BEGIN and END in RubyStopping an iteration in ruby, and Changing the Way an Object Iterates in Ruby.

You can also refer to the Official Documentation and Ruby Koans.

Also, visit our Guided Path in Coding Ninjas Studio to learn about such content. If you are preparing for an interview, visit our Interview Experience SectionTo become more confident in DSA, try out Interview Problems in our Code studio.

Thank you

 I wish you all the best for your future adventures and Happy Coding.

Live masterclass