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 provides execution of one of two statements (or blocks), depending on the condition.
Syntax
if(condition)
{
statements;
...
...
}
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
package test;
import java.util.Scanner;
public class Test
{
public static void main(String args[])
{
int b,c;
Scanner scnr = new Scanner(System.in);
System.out.println("b is : ");
b=scnr.nextInt();
System.out.println("c is : ");
c=scnr.nextInt();
if (b > c)
{
System.out.println("b is greater than c");
}
System.out.println("example for the comparison of two numbers");
}
}
Output
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
if (condition)
{
first statement;
}
else
{
second statement;
}
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
package demo;
import java.util.Scanner;
public class Demo
{
public static void main(String args[])
{
int a,b;
Scanner scnr = new Scanner(System.in);
System.out.println("a is : ");
a=scnr.nextInt();
System.out.println("b is : ");
b=scnr.nextInt();
if (a > b)
{
System.out.println("a is largest");
}
else
{
System.out.println("b is largest");
}
}
}
Output
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
switch (expression)
{
case 1:
{
statement;
}
break;
case 2:
{
statement;
}
break;
.
.
.
case N:
{
statement;
}
break;
default:
{
statement;
}
break;
}
Example
package demo;
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
int x,y,r;
double z;
Scanner scnr = new Scanner(System.in);
System.out.print("Enter x : ");
x=scnr.nextInt();
System.out.print("Enter y : ");
y=scnr.nextInt();
System.out.println("1 : Addition");
System.out.println("2 : Subtraction");
System.out.println("3 : Multiplication");
System.out.println("4 : Division");
System.out.print("Requirement : ");
r=scnr.nextInt();
switch(r)
{
case 1:
{
z=x+y;
System.out.println(z);
}
break;
case 2:
{
z=x-y;
System.out.println(z);
}
break;
case 3:
{
z=x*y;
System.out.println(z);
}
break;
case 4:
{
z=x/y;
System.out.println(z);
}
break;
default:
{
System.out.println("Requirement is invalid");
}
}
}
}
Output
Summary
This article explains the selection statements in Java.