logo codesdope

If and else


From here your real programming learning will start. In this chapter, you will learn about decision making. This means that if a user gives you some input, then you will learn to judge that input and then perform according to it.

In real life, we also make conditional decisions e.g.- If it is raining, then you will take umbrella, else not; if it is your birthday, then it is a special day, else a normal day; if you want to allow some features of a website to its users having an account, then you can think of - if the user has account then this, else not.

if and else in ruby

Now, let's implement this in Ruby.

a = gets.chomp.to_i
if a > 10
  puts "Your number is greater than 10"
else
  puts "Your number is not greater than 10"
end
Output
12
Your number is greater than 10

In the first line, we are taking input.
if a>10 - Here we used 'if'.
First see a>10 - The condition a>10 is comparing a with 10 and if a is greater than 10, then it is true and if smaller then false.
So, the whole story is that if a>10 ( if a is greater than 10 ), then it will execute puts "your number is greater than 10", otherwise it will execute puts "your number not greater than 10"
end at the last of the code indicates the end of 'if' and 'else'. You can also say that it indicates the body of 'if' and 'else'. It will be clearer from examples. So, let's proceed for now.

See the flow diagram below.

if condition
  statement
  statement
  ...
else
  statement
  statement
  ...
end

We first write if condition ( condition -> What has to be judged. In the first example, the condition was 'a>10' ). Statements under if will be executed only when this condition is true otherwise the statements under else will be executed. end at the last indicates the termination of 'if' and 'else'.

Arithmetic operator


Commonly used arithmetic operators are:
+     -     addition
-     -     subtraction
*     -     multiplication
/     -     division
%     -     modulus
**     -     exponentiation

Addition, multiplication and subtraction are simple mathematical operators with no change. Let's see an example.

a = 20
b = 10
puts a+b
puts a-b
puts a*b
Output
30
10
200

division operator (/)

First see an example.

puts 5/2
puts 5.0/2
puts 5.0/2.0
puts 5/2.0
Output
2
2.5
2.5
2.5

5/2 gave us 2 and not 2.5. And this is the thing that I was trying to show you. If both operands of / are integers, then it will also give an integer rounded to the nearest lower integer. So, 1/2 will give 0 ( as the nearest lower integer near to 0.5 is 0 ). To get float, at least anyone of the two operands must be a float.

If you want to convert an integer into a float in the middle of your code then you can use to_f as discussed in the previous chapter.

% operator gives us remainder after division of two numbers
** operation is the exponential operator. It will calculate the power of the first operand raised to the second. Ex - 4**2 means 42

Let's see an example of these operators

a = 5
b = 2
puts a%b
puts a**2
Output
1
25

As we have seen, % calculated the remainder and ** calculated 52.

Priority order


In Maths, you might have learned about BODMAS rule, but that rule is not applied here. If we have written more than one operation in one line, then which operation should be done first is governed by PEMDAS.

PEMDAS means :
Parentheses have the highest priority. It means brackets i.e. ( ).
Next is Exponential.
Then Multiplication and Division have same precedence.
Then Addition and Subtraction have same precedence.

Now a valid question comes in mind - which operator should be considered first if they have the same precedence?
The answer is that if operators have the same precedence, then they are evaluated from left to right. E.g. - 3-2+2 will be evaluated from left to right. It means that 3-2 will be evaluated first (as it is in left) and 3-2 is 1. Thus the expression will become 1+2 and will finally get evaluated as 3.

E.g.-
8/4+3-5*6+8**2
Here, among all the operators, '**' has the highest priority. So, it will be evaluated first.
So the expression now becomes 8/4+3-5*6+64.
Solving, '*' and '/' first from left to right.
2+3-30+64
Now solving, '+' and '-' from left to right
39
So, the answer is 39

If you don't want to remember these rules, then just put the expression you want to execute first in brackets. Eg- If you want to divide the product of (2+2) and 444 by the quotient of (999/5), then write the expression as - ((2+2)*444)/(999/5). This will get evaluated as (4*444)/(999/5) and finally get simplified to 1776/199 (as 999/5 is 199 and not 199.8).

Now you are ready to go for if and else.

Odd and even example

a = gets.chomp.to_i
if a%2 == 0
  puts "EVEN"
else
  puts "ODD"
end
Output
10
EVEN

if a%2 == 0 : This is very simple to understand. a%2 will give the remainder of the division of a by 2. We know that if a number is divisible by 2, then it is even. So, if 'a' is divisible by 2, then 'a%2' will be 0.
So, the whole story is that if a is divisible by 2 (a%2==0), then puts "EVEN" will be executed otherwise puts "ODD" will be executed.

if under if


Yes, we can also use one if inside another. Let's see how to do this.

puts "Enter salary"
a = gets.chomp.to_i
puts "Enter sex M/F"
b = gets.chomp
if a>10000
  if b == "M"
    puts "Your salary is good and you are a male"
  else
    puts "Your salary is good and you are a female"
  end
else
  if b == "M"
    puts "Your salary is not so good and you are a male"
  else
    puts "Your salary is not so good and you are a female"
  end
end
Output
Enter salary
12000
Enter sex M/F
F
Your salary is good and you are a female

This is also called nesting. It is very simple. After giving input, firstly, the condition of top-most 'if' will be checked i.e. a > 10000. If it is true, then the statements inside this 'if' will be executed. The first statement inside this 'if' is if b == "M". So, this will be executed and if the condition of this inner if is also true, then the statements inside it will be executed otherwise the statments inside the corresponding else will be executed.

So, after giving inputs of '12000' and 'F', the 'if' with the condition 'a > 10000' is true, so the statement inside it will be executed. Now inside it, the first statement is b == "M" and it is false. So, the statement inside the corresponding 'else' will be executed i.e. puts "Your salary is good and you are a female".

Notice that every 'if' and 'else' clause is closed with end

Using elsif


elsif is used for else if. Imagine that there are 10 pockets of candies and each of them costs 1 coin. Now, if you have more than 10 coins, then you will buy 10. Else if you have 5 coins, then you will buy 5 pockets. Else you will buy less than 5 pockets. We do the same thing in Ruby. See this example to understand this.

puts "Enter salary"
a = gets.chomp.to_i
puts "Enter sex M/F"
b = gets.chomp
if a>10000 && b == "M"
  puts "Your salary is good and you are a male"
elsif a>10000 && b == "F"
  puts "Your salary is good and you are a female"
elsif a<=10000 && b == "M"
  puts "Your salary is not so good and you are a male"
elsif a<=10000 && b == "F"
  puts "Your salary is not so good and you are a female"
else
  puts "Enter valid input"
end
Output
Enter salary
8000
Enter sex M/F
M
Your salary is not so good and you are a male

I hope that you have already got this by just an example. Next lines will make it even clearer.
Firstly, the condition in 'if' will be checked.
If this condition is true, then only 'if' will be executed (i.e. its corresponding statement will be executed) and if it is false, then the compiler will check below.
If 'if' is false, then the condition in 'elsif' will be checked. In the same way, if this condition is true, then its statements will be executed, otherwise the compiler will come below.
If the conditions of 'if' and all 'elsif' are false, then only the statement corresponding to else(i.e. puts "Enter valid input") will be executed.

if condition
  statement
  statement
  ...
elsif
  statement
  statement
  ...
elsif
  statement
  statement
  ...
elsif
  statement
  statement
  ...
elsif
  statement
  statement
  ...
else
  statement
  statement
  ...
end

We can have any number of 'elsif' after 'if'.

In the above example, we have given inputs of '8000' and 'M'. So, the condition in 'if' ( a>10000 && b == "M" ) is false since a is less than 10000. Now coming to the next 'elsif' (a>10000 && b == "F"), it is also false. Now coming to next 'elsif' ( a<=10000 && b == "M" ), it is true because 8000 < 10000 and also 'b' is 'M'. So, the statement inside this 'elsif' got executed and 'Your salary is not so good and you are a male' got printed on the screen.

We highly recommend you to practice on this section. You will get a bunch of different problems in the practice section.
You will go through many questions including the above concepts in the practice section. If you are facing difficulty anywhere, then feel free to ask questions.

Don't worry about failure. You only have to do right once.
-Drew Houston

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.