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

18 Jan, 2022
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.  
 
if statement
if-else statement
if – elsif ladder
Ternary statement
 
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 votingif 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 votingif a >= 18  puts "You are eligible to vote."else  puts "You are not eligible to vote."
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 # variablevar = 5; # ternary statementa = (var > 2) ? true : false ;puts a
Output:  
true
 
saurabh1990aror
sumitgumber28
Ruby-Basics
Ruby-Decision-Making
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Make a Custom Array of Hashes in Ruby?
Include v/s Extend in Ruby
Global Variable in Ruby
Ruby | Array select() function
Ruby | Enumerator each_with_index function
Ruby | Case Statement
Ruby | unless Statement and unless Modifier
Ruby | Hash delete() function
Ruby | Data Types
Ruby | String capitalize() Method