Conditional Statements in Ruby

What will we do if we want to execute code only in a specific condition? The conditional statements help us here. Lets take an example, I want to check if a number is in range 0 to 100 or not.

If statement

This check a condition and if condition is true then executes the code inside it.

  1. print 'give a number: '
  2. # take a number from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # check condition
  6. if a < 100
  7. puts 'a is less than 100'
  8. end

If else statement

This check a condition and if condition is true then execute the code inside it otherwise execute the code inside the else statement.

  1. print 'give a number: '
  2. # take a number from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # check condition
  6. if a < 100 and a >0
  7. puts 'a is between 0 and 100'
  8. else
  9. # code inside else statement
  10. puts 'a is outside the range'
  11. end

If and elsif statement:

This statement check one condition if condition is false then check condition of elsif and it condition is true then run code inside that statement otherwise continue to next condition.

  1. print 'give a number: '
  2. # take a number from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # if condition
  6. if a < 100 and a > 0
  7. puts 'a is between 0 and 100'
  8. # elsif condition
  9. elsif a<1000
  10. puts 'a is less than 1000'
  11. # statement can has multiple elsif statement
  12. # finally else statement
  13. else
  14. puts 'a is out of range'
  15. end

If modifier:

This is modified one line if statement. Syntax is => code if condition

  1. print 'give a number: '
  2. # take an integer from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # if modifier statement
  6. puts 'a is less than 100' if a<100

Ruby unless statement:

This is opposite to the if statement. The code inside this statement executes when condition is false. You can assume it as an if statement with opposite condition.

  1. print 'give a number: '
  2. # take an integer from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # unless statement
  6. unless a>= 100
  7. puts 'a is less than 100'
  8. end

Ruby unless modifier:

This is modified one line unless statement.

  1. print 'give a number: '
  2. # take an integer from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # modified unless statement
  6. puts 'a is less than 100' unless a>= 100

Ruby case statement:

Case statement is a modified if elsif statement, in which we take a data value and compare that value with a number of cases.

  1. print 'give a number: '
  2. # take an integer from user
  3. # execute this code and write a number
  4. a = gets.to_i
  5. # take a data value using keyword case
  6. case a
  7. # check conditions => 1..99 means in the range of 1 to 99
  8. when 1..99
  9. puts 'a is between 0 and 100'
  10. # check from 100 to 1000
  11. when 100..1000
  12. puts 'a is between 99 and 1000'
  13. # if above two cases fail then use else statement
  14. else
  15. puts 'a is outside the range'
  16. # close case using end keyword
  17. end

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.