if else statement in java

If else statements in java are used to test some conditions. It evaluates a condition to be true or false.

There are three types of if statements.

  1. If statement
  2. If else statement
  3. If else if ladder statement

Simple If statement

For example:

Output:

Price is greater than 10

You can write single line followed by semi color after if or you can use curly braces to write block of code.

You can rewrite above code as below.

If else statement

For example:
Check if number is odd or even.

Output:

20 is even

If else if ladder statement

Before we learn about If else if ladder statement, let’s first see some conditional operator.

Conditional Operators

Java provides many conditional operators.Some of them are:

  • ==  : To check if two variables are equal
  • !=   : To check if two variables are not equal
  • <    : To check if first variable is less than other
  • <= : To check if first variable is less than or equal to other
  • >   :  To check if first variable is greater than other
  • >= :  To check if first variable is greater than or equal to other
  • && :  And operation is used to check if both conditions,used with && operator, are true
  • ||    :  or  operation is used to check if one of the conditions,used with || operator, are true

For example:

Output:

Age is between 26 and 35

That’s all about if else statement in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *