Using the if else statement?

The if else statement is used for evaluating conditions and taking different actions based on the results of those conditions. Finding the larger of two values for instance.
a=10
b=15
How shall we do it?
We will compare a with b. If a is greater than b our max will be a else the max would be b.
The flow chart

This translates directly into the following Java program.

int a=10,b=15;
int max;
if(a>b)
max=a;
else
max=b;
System.out.println(max);

What is a>b? > is an operator that compares the values a and b. Returns true if a>b and returns false if a is not greater than b.
> is a relational operator.
Other relational operators are.

Relational Operators
< >=
> <=
== !=

How shall we find the maximum of three variables?

A possible flowchart.

Written as a Java program this will be.

int a=10,b=15,c=12;
int max;
if(a>b)
          if(a>c)
          max=a;
          else
          max=c;
else
          if(b>c)
          max=b;
          else
          max=c;

This is a nested loop. The two red if else are nested within the blue if else.
Another way of solving this problem is

int a=10,b=12,c=14;
int max;
if(a>b && a>c)
max=a;
else
if(b>c)
max=b;
else
max=c;
Logical Operators(And) &&
Input One Input Two Output
False False False
False True False
True False False
True True True
Logical Operators(Or) ||
Input One Input Two Output
False False False
False True True
True False True
True True True
Logical Operators(Not) !
Input Output
False True
True False

Question
A if else statement starts with the following code.

if(a<b || a<c)

Complete this program to find the maximum of 3 numbers.
What happens if we need to find the maximum of 4 numbers or more. All of the methods that we have enumerated till now will simply become too complex. Can we find a simpler way.
Let us try a different approach. Suppose we have the following sequence of numbers.
3,1,4,6,3 and we need to find the maximum.
Start by assuming that the first number is the maximum.
max=3.
Next compare the current max with the next number.(1>3). The condition is false so do nothing.
Next, compare with 4. (4>3). The condition is true, so set max=4.
Compare with the next element 6. (6>4). max=6
(3>6) being false we shall do nothing.
The sequence has been processed and the answer is 6.
Translating to code.

int a=3,b=1,c=4,d=6,e=3;
int max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)
max=e;
System.out.println(max);

Finding the minimum value would be trivial. How about finding the middle value among three numbers.
int a=1,b=2,c=3;
b is the value in the middle because it is greater than a and less than c.There is another way b can be in the middle.
int a=3,b=2,c=1; In this case b>c and b<a. So, b can be put in the middle in two different ways.

int a=1,b=2,c=3;
int mid;
if((a>b && a<c) || (a>c) && a<b))
mid=a;
else
if((b>a) && (b<c) ||(b>c) && (b<a))
mid=b;
else
mid=c;

Question: Check if a triangle is equilateral, isosceles or scalene. Equilateral means all three sides are equal, isosceles means two sides are equal while scalene means they are all different.
Let us start analyzing the problem.
We start with three variables
int a=3,b=4,c=3;
These three are the sides of a triangle.
How to check for equilateral? We need to check for a==b and b==c and a==c. One of these conditions is redundant because a==b and b==c will automatically mean a==c.
So,

if(a==b && b==c)
System.out.println("Equilateral");

How to check for isosceles? Two sides must be equal and the others have to be not equal and this will have to be for each combination.This would translate to
a==b and a!=c and a!=b. Since a==b therefore a!=c and b!=c are equivalent so a single condition would suffice.
The full condition would be.

if((a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a))
System.out.println("Isosceles");

To check for scalene.

if((a!=b) && (a!=c) && b!=c))
System.out.println("Scalene");

The three conditions are mutually exclusive and one of these three conditions is always true. So, things can be simplified. For instance, if a triangle is not equilateral, then if any two sides are equal it can only mean isosceles.

if((a==b) && (a==c))
System.out.println("Equilateral");
else
if((a==b) ||(a==c) || (b==c))
System.out.println("Isosceles");
else
System.out.println("Scalene");

The reader should try out other possible combinations.
The following program is possibly the best solution to the problem.

int count=0;
if(a==b)
count++;
if(a==c)
count++;
if(b==c)
count++;
if(count==3)
System.out.println("Equilateral");
else
if(count==1)
System.out.println("Isosceles");
else
System.out.println("Scalene");

Another problem worth discussing is checking whether a given year is a leap year. The following chart displays a list of years some of which are leap years while others are not.

Leap Years
Year Leap Year/Not Leap Year
2000 Leap Year
2018 Not Leap Year
2016 Leap Year
1900 Not Leap Year

There are two conditions necessary for a year to be a leap year.
1. It is divisible by 400.
or
2. It is divisible by 4 and not divisible by 100
How do you check for divisibility? m is divisible by n if the remainder is 0. This m%n==0 is the condition for divisibility.

Leap Year Conditions
Year Year % 400==0 Year % 4==0 Year % 100!=0 Year Type
1900 No Yes No Not leap year
2000 Yes Yes No Leap year
2016 No Yes Yes Leap year
2018 No No Yes Not leap year
if(year % 400==0)
System.out.println("Leap year");
else
if((year % 4==0) && (year % 100!=0))
System.out.println("Leap year");
else
System.out.println("Not leap year");

Leave a Reply