Codementor Events

Java tutorial 4 - IF ELSE

Published Dec 19, 2020Last updated Dec 23, 2020
Java tutorial 4 - IF ELSE

In the last tutorial, we tried to understand how operators work. In this 4th tutorial, we will aim at understanding the concept of if else statements.

In case you haven't read the previous tutorial, I recommend you to first go through it and then continue with this one. I'll wait for you.

DRUM ROLL

Now, what are if else statements?
To understand this, I will give you an example

If a is greater than 5 then print "hi" else print "bye".

Here, the output is dependent upon the value of a.

Now we are going to create the same scenario using code.

    public static void main(String[] args) {
        int a = 10;
        if (a > 5) {
            System.out.println("hi");
        } else {
            System.out.println("bye");
        }
    }

Here (a > 5) is the condition.
If the condition is true, the code will print hi,
else it will print bye.

OUTPUT:

hi

And there we are

I think that you were able to understand how if else statements work. In case you have any doubts, I am happy to help you out.

In the next part, we will learn about switch statements. Hope to see you there too.
That`s it for this blog. If you wanna become a great programmer, I am there to help.

You can see my profile by clicking on my name below.

Discover and read more posts from Aniket Malik
get started
post commentsBe the first to share your opinion
Show more replies