Java Control Statements



If Else Statement in Java:

In If Else statement a series of Boolean values become Check when condition become true that block of code Execute.

If Condition - In Java program when if condition become true then if block code become Execute.

Else Condition- When if condition Become false then else block code Execute  

Else If Condition- Else if statement is used to Check New condition in Else Block.  

      
Control Statement Flow:




If Else Statement Example:

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


Output:

Compiling the source code....
$javac My_First_Test.java 2>&1


Executing the program....
$java -Xmx128M -Xms16M My_First_Test

Value of X is 30


Switch Statement in Java:

In switch, statement a variable is checked from list of Condition or values. Each value is called a case, and the variable being switched on is checked for each case.

Switch Statement Flow:




Important Note About switch statement −

  • Variable use in a switch statement only be integers, convertable integers (byte, short, char), strings and enums.
  • You can use any number of case statements within a switch.
  • 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.
  • Not every case needs to contain a break. 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 Statement Example:

public class Test {
 
   public static void main(String args[]) {
      // char grade = args[0].charAt(0);
      char grade = 'C';
 
      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);
   }
}
Output
Well done
Your grade is C


Let’s us understand Difference between If Else and Switch Statements

if-else Statements
Switch Statements
if-else can be used for a range of values
switch-case is valid only for cases where we have a specific set of outputs
if-else can handle integers, not fractional values.
switch-case only accepts integers, not fractional values.
if-else is difficulty to when large number of conditions.
switch case is Easily readability and understand for a large number of conditions.
Where to use If-else: When simple and Few Comparisons
Where to use Switch:  when Complex and large Comparisons.


EmoticonEmoticon