If Else, Decision Making/Conditional Constructs/Branching Statements in Java



Construct are

  • simple if statements
  • if else statements
  • Nested if else statements
  • else if statements
  • switch case

Simple if Statement

It consists of a Boolean expression followed by one or more statements.

The syntax of an if statement is:

if(Boolean_expression) { //Statements will execute if the Boolean expression is true }

If the Boolean expression evaluates to true then the block of code inside the if statement will be executed.

Simple if Example

public class Demo {

   public static void main(String[] args){
      int x = 5;

      if( x < 10 ){
         System.out.print("Simple If ");
      }
	System.out.println("ThankYou");
   }
}

OUTPUT

Simple If

Thankyou

As you see both statement is displayed .As second statement System.out.println("ThankYou"); is not associated with if so no matter wheather if statement is true or false, ThankYou have to be displayed.

It means if bollean expression is true then statement associated with it will execute and if boolean expression is false then statement associated with it will be skiped.

if...else Statement

When a coin is tossed on a plain surface so either head comes or tail comes both head and tail can't come at one time.

Similarly in case of if...else if boolean expression evaluates to true then statements associated with if executes or if boolean expression evaluates to false then statement associated with else executes.

The syntax of an if...else is

if(Boolean_expression) { //Executes when the Boolean expression is true } else { //Executes when the Boolean expression is false }

if else Example

public class IfElseDemo {

   public static void main(String[] args){
      int x = 30;

      if( x < 20 ){
         System.out.print("This is if statement");
      }else{
         System.out.print("This is else statement");
      }
   }
}

Output

This is else statement

Nested if...else Statement

It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.

The syntax for a nested if...else is as follows:

if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true } else { // statement } }

Nested if else Example.Find Greatest no among three no.

public class Test {

   public static void main(String args[]){
      int a,b,c;
		a=10,b=20,c=30;
     
	if(a>b)
	{
		if(a>c)
		{
		System.out.println("A iS Greatest");	
		}
		else
		{
		System.out.println("C iS Greatest");	
		}
	}
	else
	{

		if(b>c)
		{
		System.out.println("B iS Greatest");	
		}
		else
		{
		System.out.println("C iS Greatest");	
		}
	}


    }
}

if...else if...else Statement

Once an boolean expression succeeds, none of the remaining else if's or else's will be tested.

The syntax of an if...else if is

if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true }else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true }else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true }else { //Executes when the none of the above condition is true. }

if..else if...else Example

public class ElseIfDemo{

   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("This is else statement");
      }
   }
}

Output

Value of X is 30


switch Statement

switch case can be used when user have multiple no of choices and on the basis of user choice atleast one of the case will execute.

The syntax of switch case is

switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements }

The following rules apply to a switch statement

  • The variable used in a switch statement can only be a String, byte, short, int, or char.
  • You can have any number of case statements within a switch.
  • Each case is followed by the value to be compared to and a colon.
  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • breakis not mendatory. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

switch case Example

public class SwitchDemo {

   public static void main(String args[]){

      String month = "March";

      switch(month)
      {
         case "January" :
            System.out.println("January has 31days"); 
            break;
         case "February" :
		System.out.println("February has 29 days"); 
  
	    case "March" :
            System.out.println("March has 31days"); 
            break;
         case "April" :
		System.out.println("April has 30 days"); 
            break;
         case "May" :
            System.out.println("May has 31days"); 
            break;
         case "June" :
		System.out.println("June has  30 days"); 
            break;
         case "July" :
            System.out.println("July has 31 days"); 
            break;
         case "August" :
		System.out.println("August has 31 days"); 
            break;
         case "September" :
            System.out.println("September has 30 days"); 
            break;
         case "October" :
		System.out.println("October has 31 days"); 
            break;
         case "November" :
            System.out.println("November has 30 days"); 
            break;
         case "December" :
		System.out.println("December has 31 days"); 
            break;

         default :
            System.out.println("Invalid Month");
      }

   }
}