Selection Statements in Java

Introduction

 
In Java, these are used to control the flow of the program. These are the types of selection statements in Java.
  • If statement 
  • If-else statement
  • Switch statement

If Statement

 
In Java, "if" is a conditional statement. It will provide execution of one of two statements (or blocks), depending on the condition.
 
Syntax
  1. if(condition)  
  2. {  
  3.     statements;  
  4.     ...  
  5.     ...  
  6. }  
If the condition is true then the statements inside the if block will be executed. The execution will be continued to the next statements after the execution of the statements in the if block. If the condition is false then the statements inside the if block will not be executed and the execution will start from the statements that are next to the if block.
 
Example
  1. package test;  
  2. import java.util.Scanner;  
  3. public class Test  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         int b,c;  
  8.         Scanner scnr = new Scanner(System.in);  
  9.         System.out.println("b is : ");  
  10.         b=scnr.nextInt();  
  11.         System.out.println("c is : ");  
  12.         c=scnr.nextInt();  
  13.         if (b > c)  
  14.         {  
  15.             System.out.println("b is greater than c");  
  16.         }  
  17.         System.out.println("example for the comparison of two numbers");  
  18.     }  
  19. }  
Output
 
if statement in java 
 

If-else Statement

 
If the condition is true then the statements inside the if block will be executed and if the condition is false then the statements inside the else block will be executed.
 
Syntax
  1. if (condition)  
  2. {  
  3.     first statement;  
  4. }  
  5. else  
  6. {  
  7.     second statement;  
  8. }  
If the condition is true then the first statement will be executed and if the condition is false then the second statement will be executed.
 
Example
  1. package demo;  
  2. import java.util.Scanner;  
  3. public class Demo  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         int a,b;  
  8.         Scanner scnr = new Scanner(System.in);  
  9.         System.out.println("a is : ");  
  10.         a=scnr.nextInt();  
  11.         System.out.println("b is : ");  
  12.         b=scnr.nextInt();  
  13.         if (a > b)  
  14.         {  
  15.             System.out.println("a is largest");  
  16.         }  
  17.         else  
  18.         {  
  19.             System.out.println("b is largest");  
  20.         }  
  21.     }  
  22. }  
Output
 
if else statement in Java 
 

Switch Statement

 
A switch statement can be easier than if-else statements. In the switch, we have multiple cases. The matching case will be executed. In the switch statement, we can only use int, char, byte and short data types.
 
Syntax
  1. switch (expression)  
  2. {  
  3.     case 1:  
  4.     {  
  5.         statement;  
  6.     }  
  7.     break;  
  8.     case 2:  
  9.     {  
  10.         statement;  
  11.     }  
  12.     break;  
  13.     .  
  14.     .  
  15.     .  
  16.     case N:  
  17.     {  
  18.         statement;  
  19.     }  
  20.     break;  
  21.     default:  
  22.     {  
  23.         statement;  
  24.     }  
  25.     break;  
  26. }  
Example
  1. package demo;  
  2. import java.util.Scanner;  
  3. public class Demo  
  4. {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         int x,y,r;  
  8.         double z;  
  9.         Scanner scnr = new Scanner(System.in);  
  10.         System.out.print("Enter x : ");  
  11.         x=scnr.nextInt();  
  12.         System.out.print("Enter y : ");  
  13.         y=scnr.nextInt();  
  14.         System.out.println("1 : Addition");  
  15.         System.out.println("2 : Subtraction");  
  16.         System.out.println("3 : Multiplication");  
  17.         System.out.println("4 : Division");  
  18.         System.out.print("Requirement : ");  
  19.         r=scnr.nextInt();  
  20.         switch(r)  
  21.         {  
  22.             case 1:  
  23.             {  
  24.                 z=x+y;  
  25.                 System.out.println(z);  
  26.             }  
  27.             break;  
  28.             case 2:  
  29.             {  
  30.                 z=x-y;  
  31.                 System.out.println(z);  
  32.             }  
  33.             break;  
  34.             case 3:  
  35.             {  
  36.                 z=x*y;  
  37.                 System.out.println(z);  
  38.             }  
  39.             break;  
  40.             case 4:  
  41.             {  
  42.                 z=x/y;  
  43.                 System.out.println(z);  
  44.             }  
  45.             break;  
  46.             default:  
  47.             {  
  48.                 System.out.println("Requirement is invalid");  
  49.             }  
  50.         }  
  51.     }  
  52. }  
Output
 
switch statement in Java 
 

Summary

 
This article explains the selection statements in Java.