Open In App

Ruby | Decision Making (if, if-else, if-else-if, ternary) | Set – 1

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program. Similarly, in Ruby, the if-else statement is used to test the specified condition. 

Decision-Making Statements in Ruby: 

if statement

If statement in Ruby is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if (condition) 
 
   # statements to be executed 
 
end  

Flowchart:
 

Example:

Ruby




# Ruby program to illustrate if statement
 
a = 20
 
# if condition to check whether
# your age is enough for voting
if a >= 18
  puts "You are eligible to vote."
end


Output: 

You are eligible to vote. 

if – else Statement

In this ‘if’ statement used to execute block of code when the condition is true and ‘else’ statement is used to execute a block of code when the condition is false.

Syntax:  

if(condition)  

    # code if the condition is true  

else  

   # code if the condition is false  
end  

Flowchart:
 

Example:  

Ruby




# Ruby program to illustrate
# if - else statement
 
a = 15
 
# if condition to check
# whether age is enough for voting
if a >= 18
  puts "You are eligible to vote."
else
  puts "You are not eligible to vote."
end


Output: 

You are not eligible to vote. 

If – elsif – else ladder Statement

Here, a user can decide among multiple options. ‘if’ statements are executed from the top down. As soon as one of the conditions controlling the ‘if’ is true, the statement associated with that ‘if’ is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:

if(condition1)  

# code to be executed if condition1is true
  
elsif(condition2)
  
# code to be executed if condition2 is true  

else(condition3)  

# code to be executed if condition3 is true  
end  

Flowchart:
 

Example:

Ruby




# Ruby program to illustrate the
# if - else - if statement
 
a = 78 
if a  < 50  
  puts "Student is failed" 
  
elsif a >= 50 && a <= 60  
  puts "Student gets D grade" 
  
elsif a >= 70 && a <= 80  
  puts "Student gets B grade"
   
elsif a >= 80 && a <= 90  
  puts "Student gets A grade"
    
elsif a >= 90 && a <= 100  
  puts "Student gets A+ grade"   
end


Output: 

Student gets B grade 

Ternary Statement

In Ruby ternary statement is also termed as the shortened if statement. It will first evaluate the expression for true or false value and then execute one of the statements. If the expression is true, then the true statement is executed else false statement will get executed.

Syntax: 

test-expression ? if-true-expression : if-false-expression 

Example:  

Ruby




# Ruby program to illustrate the
# Ternary statement
 
# variable
var = 5;
 
# ternary statement
a = (var > 2) ? true : false ;
puts a


Output: 

true 


Previous Article
Next Article

Similar Reads

Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | Set replace() function
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after rep
1 min read
Ruby | Set reset() function
The reset() is an inbuilt method in Ruby resets the internal state after modification to existing elements and returns self. The elements will be reindexed and deduplicated. Syntax: s1.reset() Parameters: The function does not accepts any parameter. Return Value: It returns self . Example 1: # Ruby program to illustrate # the reset() method # requi
1 min read
Ruby | Exception Handling in Threads | Set - 1
Threads can also contain exceptions. In Ruby threads, the only exception arose in the main thread is handled but if an exception arises in the thread(other than main thread) cause the termination of the thread. The arising of an exception in a thread other than the main thread depends upon abort_on_exception method. The default value of abort_on_ex
2 min read
Ruby | Set &gt;= method
The &gt;= is an inbuilt method in Ruby that returns true if a set if a superset of another set. If it is not a superset, then it returns false. Syntax: s1 &gt;= s2.name Parameters: The function takes a mandatory parameter s2 which is checked for superset of s1. Return Value: It returns true if set s2 is a superset of s1, else it returns false. Exam
1 min read
Ruby | Set merge() function
The merge() is an inbuilt method in Ruby returns the new set after merging the passed objects into a set. Syntax: s1_name.merge(object) Parameters: The function accepts a mandatory parameter enumerable object which is to be merged for. Return Value: It returns the new set after merging. Example 1: # Ruby program to illustrate # the merge method # r
1 min read
Ruby | Set member?() function
The member?() is an inbuilt method in Ruby returns true if the set contains the given object. It returns false if it does not contains the given object. Syntax: s1_name.member?(object) Parameters: The function accepts a mandatory parameter object whose presence is to be checked for. Return Value: It returns true if the set contains the given object
1 min read
Object-Oriented Programming in Ruby | Set 1
When we say object-oriented programming, we mean that our code is centered on objects. Objects are real-life instances that are classified into various types. Let’s take an example to understand this better. If we consider a rose as an object, then the class of the rose will be flower. A class is like the blue-print of an object and describes the a
9 min read
Ruby | Set include?() function
The include?() is an inbuilt method in Ruby returns true if the set contains the given object. It returns false if it does not contains the given object. Syntax: s1_name.include?(object) Parameters: The function accepts a mandatory parameter object whose presence is to be checked for. Return Value: It returns true if the set contains the given obje
1 min read
Ruby | Set delete? function
The delete?() is an inbuilt method in Ruby which deletes the given object from the set and returns the self object. In case the object is not present, it returns nil. Syntax: s1.name.delete(object) Parameters: The function takes a mandatory parameter object which is to be deleted. Return Value: It returns self after deletion of the object from the
1 min read