Friday 30 October 2015

Java Selection | If, Else If and Else


There comes a stage in life when you have grown up and begun to make your own choices, do your own thing and select what you want and not what others want.

Yea!! That stage feels really good.
The same thing applies to Java programming Language and we are at that stage.

Before I move into Java selections properly I want to discuss a few things about logical  and relational operators.

Just as Java has arithmetic operators that makes computation possible Java also has logical and relational operators that makes decision making in programming a dream come true.

Examples of Relational operators are:


==    -    Equal To
>     -    Greater Than
<     -    Less Than
>=    -    Greater Than or Equal To
<=    -    Less Than or Equal To
!=     -     Not Equal To

Examples of Logical Operators are:

&&    -    AND
||    -    OR
!     -    NOT


AND Logical Operator

If this logical operator is used, for it to be true, all the conditions must be true. Even if all is true but one is false. It becomes false.

if ((2 > 1) && (3 < 9)){
// True, therefore all statements in this block is carried out.
}

if ((2 > 3) && (3 < 9)){
// False, therefore it moves to the next statement after the IF block
// In other words, it does not carry out any statement in this block.
}

OR Logical Operator

If this logical operator is used, for it to be true, at least one of the conditions must be true. For it to be false, it means that all the conditions are false.

if ((3 > 9) || (2 < 7)){
// True because one of the conditions is true.
// Therefore, all the statements in this block is carried out.
}
if ((3 > 9) || (2 > 7)){
// False because not even one of the conditions is true.
// Therefore, no statement in this block is carried out.
}

NOT logical operator

if this logical operator is used, for it to be true, the condition must be false and for it to be false, the condition must be true.
Note that this logical operator just makes use of one condition.

if (!(3 < 9)){
// False because the condition is true
// Therefore, no statement in this block is carried out.
}

if (!(2 > 8)){
// True because the condition is false
// Therefore, the statements in this block is carried out.
}



Now that you know these logical and relational operators.

Let's get on with what I have for today.

IF BLOCK

if is a reserved word or keyword that is majorly used in selection.

Take this problem now as an example.

I want to build a web application that allows only people that are 18 years old and above to use. So I want the user to tell me his or her age to know if I can give the user access to this web application. If the user's age is 18+. Nice! the user gets to use the app but if the user is not, nothing shows. What do I do?

Here is a program of how to do it

package nicholas;
import java.util.Scanner;
public class AgeAccess{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Please, Enter Your Age: ");
int age = input.nextInt();
// check if his or her age is eligible
if (age >= 18){
System.out.println("Welcome, you have access to this web application.");
}
}
}

That's all.

Running this program now, Once the user inputs his or her age. If he or she is 18+ the user receives the message:
Welcome, you have access to this web application.
But if the user's age is not up to 18, the user sees nothing.


IF... ELSE BLOCK

From our previous example, displaying nothing on the screen for users whose ages are below 18 may get the job done but it is not the right way to pass information to them. That's why we have something to aid us in our selection known as the ELSE block.

Else block is where statements to be used by the computer are, when none of the conditions in the IF block is true. Note that there is no condition in the else block.

Returning to our problem now, you see that the ELSE block has made room for us to tell the users who is not up to 18 years old that they do not have access to the web application.

Now here is what the new program looks like:

package nicholas;
import java.util.Scanner;
public class AgeAccess{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Please, Enter Your Age: ");
int age = input.nextInt();
// check if his or her age is eligible
if (age >= 18){
System.out.println("Welcome, you have access to this web application.");
}
else{
System.out.println("Sorry, you do not have access to this web application.");
}
}
}

Running this new code now. You see that users whose ages are below 18 years would now receive a message thus:
"Sorry, you do not have access to this web application" which is way more cooler than showing nothing.

IF... ELSE IF... ELSE BLOCK

Else IF block is used in selection when IF and Else is not enough to make our selections. Else If makes use of conditions like If and unlike else.

For example: I am a teacher and I want an app that can grade the scores (no decimal point approximation) of my students in A, B, C and F
A ranges from 70 - 100
B ranges from 55 - 69
C ranges from 40 - 54
F ranges from 0  - 39

How do I write the program or create an App to do this?
If you look at this problem you will see that the use of only IF and ELSE is not enough. Therefore, making use of else if is our next and only choice.

Here is the source code or program:

package nicholas;
import java.util.Scanner;
public class GradeApp{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter The Student's Score: ");
double grade = input.nextDouble();
// select the grades
if ((grade >= 70) && (grade <= 100)){
System.out.println("The student's grade is A.");
}
else if ((grade >= 55) && (grade < 70)){
System.out.println("The student's grade is B.");
}
else if ((grade >= 40) && (grade < 55)){
System.out.println("The student's grade is C.");
}
else if ((grade >= 0) && (grade < 40)){
System.out.println("The student's grade is F.");
}
else{
System.out.println("Wrong Score Input");
}
}
}
As you can see we made use of && logical operator to enhance our selection. Always know that IF comes first, then ELSE IF and finally ELSE. Also note that there are sometimes you may not need the ELSE block just IF and ELSE IF and that is called ELSELESS IF.

In the program above as the teacher I may mistakenly put 120 as the score or -10 of which from the problem definition the score must be 0 to 100 so the ELSE block is there to catch those mistakes and tell me that it is a wrong score input.

I love this and I hope you are feeling Java gradually.

This is all for now.

Peace!!

1 comment: