Tutorial: how to use if and else in Java

In this tutorial I'm going to explain to you how you can use if and else in Java language. I suppose that you are beginner in Java language.

If and Else in Java works in the same mode as in other languages.

If(a < b)
{

}
else if{

}
else{

}

I will write a simple example:

public class Main {
public static int number = 50;
public static void main(String[] args)
{

if(number < 100)
{
System.out.println("Number is "+number);
}
else if(number == 100)
{
System.out.println("Number is "+number);

}
else
{
System.out.println("Hello World!");
}

}
}

As you can see, if number is less than 100, it shows:

System.out.println("Number is "+number);

If number is equal to 100, it shows:

System.out.println("Number is "+number);

And if number is bigger than 100, it shows:

System.out.println("Hello World!");

Video tutorial:


Java if and else by firsta-lasta-1









Leave a Comment