Home » Ruby programming

Conditional statements in Ruby

Ruby conditional statements: Here, we are going to learn about the various conditional statements with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on July 26, 2019

Ruby conditional statements

Conditional statements are also known by the name of conditional processing or conditional expressions. They are used to perform a certain set of instructions if a specified condition is met. The conditions are generally of Boolean type and return either true or false as the result. Conditionals are formed using if or case with some comparison operators. A conditional assists data about where to move next. In Ruby, 0 is considered as true whereas in other programming languages it is considered false.

Following are the basic type of conditional statements which are supported by Ruby:

  1. If statement
  2. If else statement
  3. short if statement
  4. If else if statement
  5. Unless
  6. Case statement

1) if statement

It is the most basic type of branching statement you get to know during programing. In the simpler words, it means if you find this true, do something, and otherwise do something else. They are quite easy to write.

Syntax:

    if (condition)
        #statements
    end

Example:

puts "Enter your age"
age = gets.chomp

if age.to_i >= 18
	puts "You are allowed to vote."
end

Output

First run:
Enter your age
23
You are allowed to vote.

Second run:
Enter your age
12

2) if else statement

if else statement is used to indicate what should happen next if the condition fails to be satisfied. It comes with an additional 'else' with it.

Syntax:

    if (condition)
	    #instructions
    else
	    #instructions
    end

In the above, if..end example we can observe that nothing is showing to the user as the message if the user fails to be older than 18. Let us modify that code and informs the user by using else keyword.

Example:

puts "Enter your age"
age = gets.chomp

if age.to_i >= 18
	puts "You are allowed to vote."
else
	puts "You are younger than 18 years."
end

Output

First run:
Enter your age 
23 
You are allowed to vote.

Second run:
Enter your age 
12 
You are younger than 18 years.

3) Short-if statement

Its functioning is very similar to if...else statement. It is a ternary operator and used for computing result just in one line which saves space and eventually reduces the line of code. It is recommended to be used in small tasks.

Syntax:

    result = (condition) ? (expression-if-true) : (expression-if-false)

Example:

puts "Enter marks:"
marks = gets.chomp.to_i

result = (marks>45)?'pass':'fail'

puts "#{result}"

Output

First run:
Enter marks: 
67 
pass

Second run:
Enter marks: 
12 
fail

In the above example, you have observed that we can compute the result in a single line and we are storing the result in a variable. It is also known as ?: operator.

4) if else if statement

The if-else if statement provides great help when you have more than two conditions. If the if condition is evaluated as false then the pointer will jump to elsif condition and so on. Remember that, in syntax, it is elsif (an else without 'e').

Syntax:

    if expression
        #code block.
    elsif  expression2
        #code block.   
    elsif  expression3
        #code block.   
    else
        #code block
    end 

Example:

puts "Enter fruit:"
fruit = gets.chomp

if fruit=="banana"
	puts "Outstanding Choice!"
elsif fruit=="Apple"
	puts "An Apple a day, keeps the doctor away!"
elsif fruit=="Grapes"
	puts "Good in taste"
else
	puts "I can't get you"
end

Output

First run:
Enter fruit:
Grapes
Good in taste

Second run:
Enter fruit:
Apple
An Apple a day, keeps the doctor away!

5) unless

The unless statement is a converse of if statement. If statement is evaluated when the condition turns out to be true but in the case of unless statement, the code block it contains will only be executed when the condition results to be false.

Syntax:

    unless condition
	    #code block
    end

Example (a):

flag = false

unless flag
	puts "flag is false"
end

Output

flag is false

Example (b):

flag = true

unless flag
	puts "flag is false"
end

Output

Note: No output will be there because now flag is 'true'.

6) case statement

case statement can be interchangeably used with if..elsif..end statement. We use a keyword when for implementing case conditionals. Case statement makes our code more readable.

Syntax:

    case (variable name)
    when (condition)
        #statements
    when (condition)
        #statements
    else
        #statements
    end

Example:

puts "Enter fruit:"
fruit = gets.chomp

case fruit
when "Banana"
	puts "Outstanding Choice!"
when "Apple"
	puts "An Apple a day, keeps the doctor away!"
when "Grapes"
	puts "Good in taste"
else
	puts "I can't get you"
end

Output

First run:
Enter fruit: 
Banana 
Outstanding Choice!

Second run:
Enter fruit: 
Apple
An Apple a day, keeps the doctor away! 

Observe that we have implemented the same logic using case statement as we have done in if..elsif..end statement.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.