Java – Decision Making

In the programming languages, all the line of the code is an instruction to the computer. The computer follows that instruction and performs the task. But there is a disadvantage of the computer that it can’t take the decision itself. So for the decision making, programming languages use if-else and switch-case statement.

Java If Statement

In Java, if block is a conditional statement and only executes when a condition is true. If we want to executes a block of code, only when a condition is satisfied then if statement in Java is best suitable for this.

Syntax: If statement

if(condition)
	//one line statement

Or you can write above if statement syntax in Java as below also.

if(condition)
{
	//multiple line statements
	//another statement
}

For the single line statement, the curly bracket is optional, but curly brackets are must for multiple line statements in Java.

Example: Implementation of if statement.

import java.util.Scanner;
class Example
{
	public static void main(String arg[])
	{
        Scanner in=new Scanner(System.in);
		int marks;
		//accepting user input
		System.out.print("Enter Your Marks : ");
		marks = in.nextInt();
		//condition test
        //if block will executes only when the condtion will true
		if(marks<60)
		{
			System.out.println("You need more practice");
		}
		System.out.println("Total Marks : "+marks);
	}
}
Output:
Enter Your Marks : 55
You need more practice
Total Marks : 55

Java If-Else

Suppose you have two different blocks of code to be executed based on the two different situations. For this if-else statement is used in Java, if the condition is true if block will execute or, else block will execute (if the condition is false).

Syntax: If-Else statement.

if(condition)
{
	//executes only when the condition is true
}
else
{
	//executes when the condition is false
}

In the above syntax, either if or else block will execute depends on the condition. If the condition is true then if block will execute and if the condition is false then else block will execute.

Example: How to use if-else in Java?

import java.util.Scanner;
class Example
{
	public static void main(String arg[])
	{
        Scanner in=new Scanner(System.in);
		int num;
		//accepting user input
		System.out.print("Enter a Number : ");
		num = in.nextInt();
		//condition to test given number is even or odd
		//if the remainder is 0 after deviding by 2 then it is even
		if(num%2 == 0)
		{
			System.out.println("Even Number");
		}
		else
		{
			System.out.println("Odd Number");
		}
	}
}
Output:
Enter a Number : 34
Even Number

Java If-Elseif-Else

Java if-elseif-else statement is helpful when we have more than two conditions to test and want to execute a different block of code for different conditions.

Example: How to use if-else if-else statement in Java?

import java.util.Scanner;
class Example
{
	public static void main(String arg[])
	{
		Scanner in=new Scanner(System.in);
		int num;
		//accepting user input
		System.out.print("Enter a Number : ");
		num = in.nextInt();
		//condition to test given number is positive, negative or zero
		//greater then 0 is positive, less then 0 is negative, else it is zero
		if( num>0)
		{
			System.out.println("Positive Number");
		}
		else if( num<0 )
		{
			System.out.println("Negative Number");
		}
		else
		{
			System.out.println("Zero");
		}
	}
}
Output:
Enter a Number : -45
Negative Number

Nested If in Java

In Java, nested if is a statement within if or else block. You can find a better answer in the given below example.

class Example
{
	public static void main(String arg[])
	{
		int a=1, b=2, c=3;
		if(a>b)
		{
			if(a>c)
				System.out.println("First Largest");
			else
				System.out.println("Third Largest");
		}
		else if(b>c)
		{
				System.out.println("Second Largest");
		}
		else
		{
			System.out.println("Third Largest");
		}
	}
}

Switch Case in Java

In Java, the switch case is also used for decision making. It is useful when we have multiple options for a single variable and we want to execute a block of code based on user’s choice.

Syntax: switch case in Java.

switch(variable)
{
	case 1:
		//statement
		break;
	case 2:
		//statement
		break;
	case 3:
		//statement
		break;
	case 4:
		//statement
		break;
	default:
		//statement
}

Some Points about Switch Case

  • The switch statement can have any number of case.
  • All case must end with a colon ( : ).
  • The switch can accept numbers or characters values.
  • default: executes only when no case value is matched.
  • the default is optional, you can skip in your program.
  • break: end processing and exit from the switch.

Example: example of a switch case in Java.

import java.util.Scanner;
class Example
{
	public static void main(String arg[])
	{
		int ch;
		Scanner in=new Scanner(System.in);
		System.out.print("Enter a number(between 1 to 7) : ");
		ch=in.nextInt();
		switch(ch)
		{
			case 1:
				System.out.println("Sunday");
				break;
			case 2:
				System.out.println("Monday");
				break;
			case 3:
				System.out.println("Tuesday");
				break;
			case 4:
				System.out.println("Wednesday");
				break;
			case 5:
				System.out.println("Thursday");
				break;
			case 6:
				System.out.println("Friday");
				break;
			case 7:
				System.out.println("Saturday");
				break;
			default:
				System.out.println("Not a day");
		}
	}
}
Output:
Enter a number(between 1 to 7) : 5
Thursday