Conditional Statements

Conditional Statements

by Sanjay Nair February 17, 2018

There are various types of conditional statements in Java.In this tutorial, we will discuss about the following conditional statements.

  • if-statement
  • if-else statement
  • if-else if ladder
  • nested if statement

If Statement

  • The java if statement tests any particular condition. If the statement is true, then the successive block is executed.

Syntax:

if(condition)

{

//code to be executed

}

Example

Output

Age is greater than 18

if-else Statement

 

  • The if else statement also checks for condition.

 

  • But, in addition to executing like an if statement , the if -else statement executes the else block , when the condition fails.

 

Syntax

 

if(condition)

{

//code if condition is true

}

else

{

//code if condition is false

}

Example

Output:

odd number

The if-else-if ladder Statement:

 

  • The if – else-if ladder statement executes one condition from multiple conditions.

 

Syntax:

 

if(condition1)

{

//code to be executed if condition1 is true

}

else if(condition2)

{

//code to be executed if condition2 is true

}

else if(condition3)

{

//code to be executed if condition3 is true

}

else

{

//code to be executed if all the conditions are false

}

Example

 

Output:

C grade

You may also like

Leave a Reply