Skip to main content

Ruby Control Statements

Control statement are used to manage program execute is based on specific condition. In this section are given basic example which are control program execution dynamically. That is very important because there is possible a simple app are based on multiple test condition.

if statement

That is simple and selection based statement that is depending upon given expression condition. Generally most of statements are based on condition, because condition is decide the which statements allowed to the current execution. if-statement are defined by given syntax.

if condition
  #when if condition is not false

Ruby if statement are different to other programming language. Ruby if statement is based on condition result or value. When if resultant value are not in boolean (false) then this condition are execute this block statement.

#case 1
if (1>0) #conditional expression (1>0)
  puts "One is greater than zero"

#case 2
if true
  puts "true Statement work"

#case 3
if 0
  puts "O is true condition"

#case 4
if [1,2]
  puts "Array condition is true"
One is greater than zero
true Statement work
O is true condition
Array condition is true

Observe in this example when expression is conditional or simple value in this program all case are not in boolean (false) value so every statement are executing.

elsif else statement

This both statement are associative part of if statement. When if statement condition result is false. Then chance to creates of to check comparative elsif condition. And else-statement is an optional condition block that are executed when on associate if and elsif statement are not work.

#Syntax of if,elsif and else
if condition1
  #when if condition1 not false
elsif condition2
  #when if condition2 not false
elsif condition3
  #when if condition3 not false
elsif condition4
  #when if condition4 not false
  #else statement

For example

def check_data x
  if type == Range
    puts "Range are pass"
  elsif type == String
    puts "String data found"
  elsif type==Array
      puts "Found an array"
    puts "That is other type #{type}"   

check_data [1,2]
check_data "Hello"
check_data 4
check_data true
check_data 1..2
Found an array
String data found
That is other type Fixnum
That is other type TrueClass
Range are pass


This conditional statement is work on boolean (false) value. When if given conditional value and result are in boolean false then this statement are work. In other word this is an opposite to if-statement.

unless status == 100
  puts "Unless Status"
  puts "Else Statement"
Unless Status

Another example

unless status
  puts "Status is false"
  puts "Status is true"
Status is false

Case when statement

When statement are used to check multiple test condition of a single expression value.

def check_data x
  case x
  when 1..100
    puts "It's number between 1 and 100"
  when 'a'...'z'
     puts "It's char between a and z"
  when String
    puts "You passed string"
    puts "other value : #{x}"

check_data 'p'
check_data "Hello"
check_data 4
check_data true
It's char between a and z
You passed string
It's number between 1 and 100
other value : true
speed = 130

result = case speed
   when 0 then "Stop"
   when 10..50 then "Medium Speed"
   when 5..120 then "Average Speed"
   when 120..210 then "Maximum Speed"
   else "Not Define"
puts result
Maximum Speed

Logically Loops also a conditional statement because that are controlling the execution of statement based on particular test condition.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment