Decision Making And Branching
If statement
What is a decision? In simple terms it is a task done when a particular condition is met. Let us see some examples.
My father will buy me a bike if I come first in my class
She will go out with me if I have a car
Ramesh can watch the cricket match if his boss gives him the day off.
In each of the following statement the first part is the task which will be accomplished if the condition in the second part is met. This is written in English now let us see how we can write this in a more general manner
If (student comes first in class) then
Father buys a bike
End if
If (person has car) then
Girl goes out
End if
If (boss gives a day off) then
Ramesh watches the cricket match
End if
Therefore the general syntax of the Decision making statement is
If(condition)
Statements to be executed
How is it useful in our Java program, let us say that if the age of a driver is greater than 18 then he can drive and we want to print the message “You are eligible to drive”
The line if(age>=18) can have only two options either True or False. If it is true then the next line is executed if the answer is false then the next line is skipped.
The next question that might arise is what if I need more than one lines to be executed on a true condition. In the present format it only executes a single line. To overcome this drawback we can use curly brackets to signify a block of code that is executed when the condition is true.
For example look at the next program.
Please mark the difference if the condition is true then two lines are executed and to implement this situation we use the braces to create a block of code or a body.
The next genuine question should be what about those people who cannot drive or are below the age of 18. To add a false condition we use the if()…else construct. Let us first look at some simple examples in English which clearly bring out the difference.
My mother will buy Chinese food today if it does not rain else she will cook at home
My father will buy a house in Bombay if he gets a loan else he will look for something in his hometown.
I will try to become a doctor if I clear the medical exams else I will try for my PhD.
To represent the above mentioned statements we will use if…else construct
If(it rains)
{
Buy Chinese food
}
else
{
Cook at home
}
Now let’s try this on the same java program with a message for people who are not eligible to drive. In this program if the age is greater than equal to 18 the program will print a congratulation message. On the other hand if age is less than 18 then it will print the sorry message.
I would like to show you one more example. This program prints if the given number is even or odd. As we all know an even number is completely divisible by 2 and odd number is not. Therefore our condition becomes true if number is divisible by 2 and false if the number is not divisible by 2.Let us look at one more example to print the greater of two numbers. The condition is quite simple we have to compare two numbers using a if statement.
The next program makes everything a bit more interesting, depending on your likes and dislikes there are two ways of doing it, first I will show you the easy way using logical operators and then I will show you the same program without using logical operators. In java you can use if in conjunction with else, this is used to test more than one conditions, it is particularly useful where simple if … else construct fails. In the next program you are given three sides of a triangle and your program must print if the side is an equilateral triangle, isosceles triangle or scalene triangle. For the uninitiated a triangle is equilateral if all its sides are of same length, if any two sides are the same then it is an isosceles triangle and if none of the sides are equal then it is called a scalene triangle.
As you can see if the first condition is false then it checks another condition and if that too is false then it checks the third condition.
Now let me give the alternative which happens to be my favored way of writing conditional statement. It may be a bit difficult to understand to begin with but I find it more elegant.
There are many ways in which you can use these statements, please remember there is no hard and fast rules except the syntax, beyond which it is your own imagination. Another popular if..else example is student grade program. For example if each student in your class gets grade according to the following criteria
Marks greater than or equal to 85 grade is A, marks greater than or equal to 70 Grade is B, marks greater than or equal to 50 Grade is C and marks less than 50 Grade is F.
Let us make it a bit more interesting and calculate our net salary based on the following criteria
If basic pay is more than 50,000 then HRA is 2% DA is 40% and Income Tax (IT) is 25%
If basic pay is more than 20,000 and less than or equal to 50,000then HRA is 4% DA is 50% and Income Tax (IT) is 20%
If basic pay less than or equal to 20,000 then HRA is 8% DA is 70% and Income Tax (IT) is 5%. Most important point to remember here is the calculations may give us floating point numbers therefore in this program we cannot use integer numbers.
No comments:
Post a Comment