Getting familiar with Java:: Relational operators and if else statements

So far in the series of Java articles we learnt about the first program ‘Hello World’. Then, we learn about integer, shortcuts to write system.out.println and then we see the result function of integer and then we learn more about,  mod function which is the remainder of division and then we learn about if statement. In between we do feel that there are some thing that remains to be there with if statement as the process of writing the code becomes longer.

//folder or package name

package day1.examples;

//Exampleif class name declaration

public class Exampleif {

//public method declaration

                public static void main(String[] args) {

                                //datatype integer with variables and their values declaration

                                int x = 20;

                                int y = 20;

                               

                                //first if statement of if x is less than y

                                if (x < y) {

                                                System.out.println("x is less than y");

                                }

                               

                                //second if statement declaration of if x is equal equal to y

        if (x == y) {

                System.out.println("x is equal to y");

        }

       

        // third if statement declaration if x is greater than y

       

        if (x > y) {

                System.out.println("x is greater than y");

        }

                }



}

If you notice about if statement in the previous article this is an not so efficient way of learning and writing the code. The program is checking if x is less than y, if x is equal to y and if x is greater than y and then come to the conclusion. Here, all these three statements runs and then compilation of code follows. We can use else statement in order to simplify the entire process.  Now, it will come and check else statements and if that condition does not matches then it will run the next else statement.

For adding else statement our new code will be like this.

 

//folder or package name

package day1.examples;

//Exampleif class name declaration

public class Exampleif {

//public method declaration

                public static void main(String[] args) {

                                //datatype integer with variables and their values declaration

                                int x = 20;

                                int y = 20;

                               

                                //first if statement of if x is less than y

                                if (x < y) {

                                                System.out.println("x is less than y");

                                }else if (x == y) {

                System.out.println("x is equal to y");

        }else if (x > y) {

                System.out.println("x is greater than y");

        }

                }



}

 

Now, ours code is if x is less than y then,  print x is less than y and then if not or else if then check of x is equal to y and if not or else if check for x is greater than y. Due to add on of else if function we come to understand that,  not each and every if statement have to be run in order to find the print statement. It does nor run all of these three print statement, as it checks if it does not satisfy the criteria then it turns back to the next statement and thus, it saves some precious seconds for the program.

In a sense when you compile the code and run it, the same result of as of previous that of f statements but here we find that most of times the running of print statement in each and every moment does not come into reality. Here we can shorten this else if code further as follows. In earlier case of if and else statement as written above we made slight changes to the if else code so as to make it more precise.

if (x < y) {

                                                System.out.println("x is less than y");

                                }else if (x == y) {

                System.out.println("x is equal to y");

        }else if (x > y) {

                System.out.println("x is greater than y");

        }

                }

In the last else if statement which is of x>y, here we do not need to put else if as in the first if statement if the condition is not right then move to the second statement and if that is not right then we do not need if statement in the third condition as it only needs else as the third statement should be true in this case as both the first and second statement does not matches the logic and this means that in the third if statement we do not need to put else if as we need only to put else here.

So ours new code should be like this.

if (x < y) {

                                                System.out.println("x is less than y");

                                }else if (x == y) {

                System.out.println("x is equal to y");

        }else {

                System.out.println("x is greater than y");

        }

So as of now we do see that there is no need to put else if statement in fully, as only else suffice in this case as it provides, else and here only print statement is suffice as it remains to be the only solution apart from all of these three statements. That is why we have deleted if (x > y) in the last statement.  For the sake of checking the code you can save then run as and then run the java application. The output or compilation will be the same as that before.

As programmer there is extreme importance should be given to small and small developments such as when to put the semi colons and when to put the curly braces and so on. As and when  you develop java program, you will with experience find the information about it, when and where to put,  semicolon and curly braces and so on. For the simple experience a complete statement ends with a semicolon.

int x = 20;

                                int y = 20;

 

here above both of these statements are full so ends with semicolon. Where as when we look at the if statement, at the end of first statement there is no semicolon, as it is not a complete statement.

if (x < y) {

                                                System.out.println("x is less than y");

                                }else if (x == y) {

                System.out.println("x is equal to y");

        }else {

                System.out.println("x is greater than y");

        }

Here, if x is less than y then it is not a complete statement, as there are else statements out there and  for this after system.out.println we find that there is semicolon at the end of the statements. Similarly, in the method statements such as

public static void main(String[] args)

here there is no semicolon as this is a method and the declaration of variables are still out there. In the case of presence of curly braces, each and every curly braces are there in dual mode and you can check the presence of its corresponding curly braces by clicking on one curly brace and then the related curly braces have the rectangle out there to suggest that these two are relational curly braces. In this way, you will remove the confusion of curly braces and make sure that every open or close curly braces are closed or opened properly. These symbols () are called as parenthesis, the less than symbol > or the greater that symbol > or equal to symbol == are relational operators.

Here, two equal to == signs are equal to and then exclamation and equal to != is not equal to, >= is greater than or equal to and then <= is less than or equal to signs.

These are few of the smallest symbols to understand the presence of such small and little bit of things that should have helped you immensely in dealing with coding infrastructure of Java.

This entry was posted in Java on by .

Modified: 29th Oct 2017

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.