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.

Related Posts

  • 27 April

    Switch case in java

    Table of ContentsSyntax of Switch case in javaSwitch case String example Switch case in java is alternative to if else if ladder. It is used to execute statements based on some conditions. Syntax of Switch case in java [crayon-60f8d9be854b0827447904/] Let’s understand it with the help of simple example. We are going to print weekday based […]

  • 27 April

    Do While loop in java with example

    Table of ContentsSyntax for do while loop in javaExerciseInfinite do while loop in java There are several looping statements available in java. One of them is do while loop in java. While loop is used to execute some statements repeatedly until the condition returns false. If the number of iterations is not known beforehand, while […]

  • 13 April

    Java break statement example

    Table of ContentsJava break statement exampleLabeled break statement: Break statement is one of the different control statements which we use very often. If break statement is found in loop, it will exit the loop and execute the statement following the loop. Let’s take very simple example: Java break statement example You want to search for […]

  • 12 April

    Java continue statement example

    Table of ContentsJava continue statement exampleLabeled continue statement continue statement is one of the different control statements which we use very often.If continue statement is found in loop, it will continue the current iteration and will not execute statements following continue statements. It is generally used to skip current iteration on the basis of some […]

  • 05 April

    While loop in java with example

    Table of ContentsThe syntax for while loop in javaExerciseInfinite while loop There are several looping statements available in java. One of them is while loop in java. While loop is used to execute some statements repeatedly until condition returns false. If number of iterations are not known beforehand, while loop is recommended. The syntax for […]

  • 05 April

    For loop in java with example

    Table of ContentsSyntax for for loop in javaExerciseInfinite for loop in javaUse two variables in for loop in java There are several looping statements available in java. One of them is for loop in java. For loop in java is used to execute some statements repeatedly until the condition returns false. There are three parts […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.