Like any language, Ruby provides conditional statements that support if, if-else, and if-else conditional decisive statements.

It provides the following features.

  • simple if statements
  • if-else statements
  • if-else if
  • unless statement

Ruby simple if statements

Simple if is a code block that executes multiple statements if the conditional expression is true. The conditional expression always results in the bool type and the possible values - true and false only. Here is the syntax of a simple if statement.

if conditional-expression
   //if code block executed
end

if and end is a keywords in Ruby that can not be used as variable names. It is followed by conditional expression.

conditional-expression is always resulting in true or false only. if it is true a code block is executed.

code block contains a single line or block(multiple lines) of code.

Simple if statements are used to test a single possible value like null check or empty object check.

Here are valid if simple statements examples.

number = 11
if number ==11
   puts "number is 11"
end

Output:

number is 11

Following also a valid code

number = 11
if number
   puts "number is 11"
end

Ruby if else statements example

if-else is to execute multiple blocks of code based on conditional expression value. The conditional expression always results in the bool type and the possible values - true and false only.

if else statements are used to execute statements based on true and false values.

Here is the syntax of the if else statement.

if  conditional-expression1
  //statements1

else
  //statements2

end

conditional-express results in a value of bool type.

if true, a statements1 inside an if block is executed, else statements2 are executed.

Here is an example code

salary = 5000;

if salary > 5000
    puts "salary is greater than 5000";
else
    puts "salary is less than 5000";
end

Output:

salary is less than 5000

Rust if else if statements example

if else if is to execute multiple blocks of code based on multiple conditional expression values. The conditional expression always results in the bool type and the possible values - true and false only.

if else statements are used to execute multiple conditional statements based on true and false values.

Here is the syntax of the if else if else statement.

if condition1
  // statements executes if condition1 is true
 elseif condition2
  // statements executes if condition1 is false and condition2 is true
else
  // statements executes if condition1 is false and condition2 is false
end

Here is an example

number = 50;
if number == 50
    puts "50";
elseif number == 100
    puts "100";
elseif number == 25
    puts "25"
else
    puts "other number";
end

In the above example, if the number is 50, print 50 to console else, if the number is 100, print 100. else if the number is 25, print 25 if none of the conditions are met, It prints “other number”.

Ruby Unless conditional statement

unless the statement is used to execute if the conditional expression is false. This can be used as simple unless and unless else conditional statements.

unless conditional-expression
   //if code block executed
else
//code executes for true
end

Here is an example program code

number = 11
unless number==5
   puts "number is not 5"
 else
   puts "number is  5"
end

Output:

the number is not 5

Ruby unless and if modifier

These are used to execute code based on conditional statement modifiers for simple conditions.

code statements if conditional-expression
code statements unless conditional-expression

if modifier executes code if the condition is true. unless modifier executes code if the condition is false.


condition1 = true

puts "condition1: if modifier result" if condition1
puts "condition1: unless modifier results" unless condition1

condition2 = false

puts "condition2: if modifier result" if condition2
puts "condition2: unless modifier results" unless condition2

Output:

condition1: if modifier result
condition2: unless modifier result