knowledgediarybd.com

Your Virtual Knowledge Campus

If Else & Switch Statement in Java

if else statement in javaIf Else & Switch Statement in java are most essential statement  for checking various logical condition.

These statement provides a easiest way to solve a problem in a short time & increase the performance of the java program code.

Dear learner, here you can learn how to use If Else & Switch Statement in java that will satisfy your interest to learn java language.

If Else & Switch Statement in Java

Decision making statements in Java:

There are two types of decision making statements in Java. They are:

1. if statements

2. switch statements

1. if Statement:

An if statement consists of a Condition followed by one or more statements.

Syntax of if statement :

if(Condition)

{

//Statements will execute if the condition is satisfied or true;

}

More details :

if state ment

If the Condition evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Example:

public class Test {

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

      if( x < 20 ){
         System.out.print("Humayun Kabir");
      }
   }
}

Output of the above code

Humayun Kabir

2.The if…else Statement:

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax if…else Statement:

 

if(Condition){

//Executes when the Condition is true;

}

else

{

//Executes when the Condition is false;

}

More details :

if..else

Example of  if…else Statement:

public class Test {

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

if( x < 20 ){
System.out.print("Humayun Kabir");
}else{
System.out.print("Mahabubur Rhaman");
}
}
}

The  result will be looks like below:

Mahabubur Rhaman

The if…else if…else Statement:

An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.

When using if , else if , else statements there are few points to keep in mind.

1. An if can have zero or one else’s and it must come after any else if’s.

2. An if can have zero to many else if’s and they must come before the else.

3. Once an else if succeeds, none of he remaining else if’s or else’s will be tested.

Syntax of if…else if…else Statement:

if(Condition 1){

//Executes when the Condition 1 is true

}else if(Condition 2){

//Executes when the Condition 2 is true

}else if(Condition 3){

//Executes when the Condition 3 is true

}else {

//Executes when the none of the above condition is true.

}

Example of if…else if…else Statement:

public class Test {

   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");
      }
   }
}

This would produce following result:

Value of X is 30

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.

Syntax of Nested if…else Statement::

if(Condition 1){

//Executes when the Condition 1 is true

if(Condition 2){

//Executes when the Condition 2 is true

}

}

You can nest else if…else in the similar way as we have nested if statement.

Example of Nested if..else statement:

public class Test {

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

      if( x == 30 ){
         if( y == 10 ){
         System.out.print("X = 30 and Y = 10");
      }
   }
}

The result will looks like below:

X = 30 and Y = 10

The switch Statement:

switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax of  switch statement:

switch(expression){

case value :

//Statements

break; //optional

case value :

//Statements

break; //optional

//You can have any number of case statements.

default : //Optional

//Statements

}

Rules of switch statement:

1.The variable used in a switch statement can only be a byte, short, int, or char.

2.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.

3.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.

4. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

5.When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

6. 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.

 More Details:

switch statement
Example of switch statement:

public class Test {

   public static void main(String args[]){
      char grade = args[0].charAt(0);

      switch(grade)
      {
         case 'A' :
            System.out.println("Excellent!"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

      }
   }
}

Note:More interested things are waiting to publish..Stay connected to us..

——————-:Thank you:—————–

Related Post or you may Like:

knowledgediarybd.com © 2017
shares