Decision Control Statements

In Java programming, decision control statements perform different computations or actions depending on whether a specific condition holds true or false. Based on it, certain decisions are to be made. They are also called  conditional statements.

We have the following decision control statements in Java:

  • if statement we use this statement if we want to execute some code only if a specified condition is true
  • if-else statement we use this statement if we want to execute some code if the condition is true and another code if the condition is false
  • if-else nesting statement we use this statement if we want to select one of many blocks of code to be executed
  • switch-case statement we use this statement if we want to select one of many blocks of code to be executed

 

1.  if statement

We should use the if statement if we want to execute some code only if a specified condition is true.

Syntax
if (condition) 
{
code to be executed if condition is true
}
Example 1.
//DecisionTest01.java
public class DecisionTest01 {

public static void main(String[] args) {
int a = 20, b = 10;

if(a > b) {
System.out.println(a + " is bigger than " + b);
}
}
}

Output:

20 is bigger than 10

 

2.  if-else statement

If we want to execute some code if a condition is true and another code if the condition is not true, use the if-else statement.

Syntax
if (condition) 
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example 2.
//DecisionTest02.java
public class DecisionTest02 {

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

// Testing for even or odd number
if(n%2==0) {
System.out.println(n + " is an even number.");
}
else {
System.out.println(n + " is an odd number.");
}
}
}

Output:
10 is an even number.

 

3.  if-else nesting statement

We should use the if-else nesting if we want to select one of many sets of lines to execute.

Syntax
if (condition1) {
code to be executed if condition1 is true
}
else if (condition2) {
code to be executed if condition2 is true
}
else {
code to be executed if condition1 and condition2 are not true
}
Example 3.
//DecisionTest03.java
public class DecisionTest03 {

public static void main(String[] args) {
int a = 30, b = 30;

if(a > b) {
System.out.println("a is bigger than b.");
}
else if(a < b) {
System.out.println("a is smaller than b.");
}
else {
System.out.println("a and b are equal.");
}
}
}

Output:

a and b are equal.

 

 4. switch-case statement

We should use the switch-case statement if we want to select one of many blocks of code to be executed.

Syntax
switch(choice)  

    case 1:    
     execute code block 1     
       break;      
case 2:    
execute code block 2     
break; 
case 3:    
execute code block 3    
break; 
default:
execute code if choice is different
break;
}

This is how it works: First we have a single expression choice (most often int or char type variable is used), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. We use break statement to prevent the code from running into the next case automatically.

Example 4

//DecisionTest04.java
public class DecisionTest04 {

public static void main(String[] args) {
int choice = 3;

switch(choice) {
case 1:
System.out.println("hello");
break;

case 2:
System.out.println("hi");
break;

case 3:
System.out.println("welcome");
break;

default:
System.out.println("bye");
break;
}
}
}

Output:

welcome

 

The switch-case statement with String

Java SE 7 has modified the “switch-case” statement to support String also. See the code snippet below:

String choice = "test";

switch(choice) {
case "hello":
System.out.println("hello");
break;

case "hi":
System.out.println("hi");
break;

default:
System.out.println("bye");
break;
}

    Output:

bye