Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - IF...ELSE


If...Else is somewhat similar to Decision Making.


To reach our office we have to take the decision that if we get a bus we will ride in it else we will be hiring a cab.


Even Ruby has to make some decisions. And there are ways to write it :

  1. if

  2. else

  3. elsif

So, when we write the above going to office by bus or cab scenario in Ruby, it would be :


Decision making using if


Example :



vehicle = "Bus";
		
if vehicle.eql?"Bus" 
	puts "Will go to office by Bus.";
end


Output :



  Will go to office by Bus.
java_Collections

In the above code we have created a String type variable vehicle and assigned the value "Bus" to it.


vehicle = "Bus";

Then we are telling Ruby. If it finds that the String type variable vehicle has value Bus. Get into the block of if statement and print Will go to office by Bus.:


if vehicle.eql?"Bus"
	puts "Will go to office by Bus.";
end

Now, let us make a small change in code :


Example :



vehicle = "Car";
if vehicle.eql?"Bus"
	puts "Will go to office by Bus.";
end


Output :



  In the above code there is no output.
java_Collections

It is because, in this case the variable vehicle is having the value Car.


vehicle = "Car";

Now, using the if statement, when Ruby checks, if the value of vehicle is Bus or not. It does not find because the value of the vehicle is Car.


if vehicle.eql?"Bus"
	puts "Will go to office by Bus.";
end

And thus it does not enter the if block and Will go to office by Bus. is not printed.


Decision making using if and else


When a condition is not met(As in the above case nothing was printed), we could use an else statement along with if. So that we can print something if the condition is not met.


It is just like, if you don't get a bus. You go by a cab.


Example :



vehicle = "Car";
		
if vehicle.eql?"Bus"
	puts "Will go to office by Bus.";
else 
	puts "Will take a Cab.";
end		


Output :



  Will take a Cab.

Since, the if condition didn't meet :


if vehicle.eql?"Bus"
	puts "Will go to office by Bus.";

It came to the else statement and printed the value Will take a Cab. inside the else statement.


else
	puts "Will take a Cab.";

Decision making using if, elsif and else


Even in the else statement we can put an if condition. Just to check if the second condition meets or not.


Say there can be a situation that if we do not get a Bus, we will try to go by a cab. And if the cab is also not available. Simple, we won't go to the office.


Example :



vehicle = "No Vehicle";
		
if vehicle.eql? "Bus"
	puts "Will go to office by Bus.";
elsif vehicle.eql? "Cab"
	puts "Will take a Cab.";
else 
	puts "Won't go to office.";
end


Output :



  Won't go to office.

In the above code, we have initialized the variable 'vehicle' with 'No Vehicle'(Suppose no vehicle is available).

java_Collections

The first condition doesn't meet, as we are checking 'vehicle' variable is holding the value 'Bus' or not.


if vehicle.eql? "Bus"
	puts "Will go to office by Bus.";

Then it comes to the else part, where there is an if statement again, checking if 'vehicle' variable is holding 'Cab' or not.


elsif vehicle.eql? "Cab"
	puts "Will take a Cab.";

It doesn't match, and it comes to the final else and prints the contents inside the 'else' block.


else
	puts "Won't go to office.";