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



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel