Java Fundamentals – Part 3

Hello guys, I am back with the 3rd article of Java Fundamentals article series. I hope you guys are enjoying and learning the concepts clearly. Today I am going to talk about Selections in Java.

If Statements

Selections are the backbone of programming. The reason why we do programming is to alter the default behaviour of something by checking a condition. If conditions are there in every programming language that has ever been invented.

        int age = 21;
        
        //Checking if the age is higher enough to vote
        if (age > 18){
            System.out.println("You can vote");
        } else {
            System.out.println("You are not old enough to vote");
        }

This is the basic structure of an if statement. We check a condition and we make the program do something if the condition is true. If not, we make the program do something else.

We can use comparison operators and logical operators when writing our conditions

Comparison Operators

  • > means greater than
  • < means less than
  • == means equals
  • != means not equal to
  • <= means less than equal to
  • >= means greater than or equal to

Logical Operators

  • & means Logical AND
  • && means short circuit AND (I’ll explain later in the article)
  • | means Logical OR
  • || means short circuit OR (I’ll explain later in the article)
  • ! means Logical NOT

The Short Circuit Concept

These operators have been introduced to improve the performance of your program. I recommend using these operators all the time.

        int age = 17;
        boolean isAlive = true;

        //Checking if the age is higher enough to vote
        if (age > 18 && isAlive){
            System.out.println("You can vote");
        }

In this code snippet, when we use the && operator, we are not going to check the second condition at all since the first condition is false. Because the compiler knows that when one condition is evaluated false, the whole condition set is going to be false regardless of the result. But if the first condition was true, the compiler would check the second condition as well.

In comparison, if we used the & operator here, the compiler would always check both conditions regardless of the result of the first condition.

Nested IF conditions

Here we are talking about if conditions inside if conditions.

        int value = 6;
        
        //Checking what the value is
        if (value > 0 && value <= 10){
            if (value < 5){
                System.out.println("The value is below 5");
            } else {
                System.out.println("The value is between 5 and 10");
            }
        }    

In this code snippet, we are not going to check inside the outer if condition if it is not true. This is going to save computer processing power and will speed up the overall process. There are much better ways to do this. I will be discussing them later.

Switch Statement

This statement is not being used that much. This is an alternative way to writing a lot of if statements

        int dayOfTheWeek = 3;

        //Checking what the value is
        if (dayOfTheWeek == 1){
            System.out.println("It is Monday");
        } else if (dayOfTheWeek == 2){
            System.out.println("It is Tuesday");
        } else if (dayOfTheWeek == 3){
            System.out.println("It is Wednesday");
        } else if (dayOfTheWeek == 4){
            System.out.println("It is Thursday");
        } else if (dayOfTheWeek == 5){
            System.out.println("It is Friday");
        }

We can alternate the above code with a switch statement.

        int dayOfTheWeek = 3;

        //Checking what the value is
        switch (dayOfTheWeek) {
            case 1 :
                System.out.println("It is Monday");
                break;
            case 2 :
                System.out.println("It is Tuesday");
                break;
            case 3 :
                System.out.println("It is Wednesday");
                break;
            case 4 :
                System.out.println("It is Thursday");
                break;
            case 5 :
                System.out.println("It is Friday");
                break;
        }

We should always add a break statement at the end of each switch block to stop condition block from being executed regardless of the condition status.


This is it for today’s article guys. Thank you for taking the time to read my article. I hope you understood the concepts clearly. If you have any doubts, feel free to drop a comment down below. If you liked the article, drop a like and share it among your friends. Don’t forget to subscribe so you won’t miss future articles. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: