Decision Making In Java

Sometimes our program needs to take a decision based on whether a particular condition has occurred or not.  Then our program will execute certain statements based on this decision.
Decision making in Java can be achieved using any of the following statements.

  1. If statement
  2. switch  statement
  3. conditional operator statement.

if statement

This statement is used to make a certain decision in our program.

For example, sometimes we need to check if salary is credited or not. If credited, we may need to pay some utility bill and if salary is not credited, we may need to wait for salary. So here, based on the condition whether salary is credited or not, we are taking a decision on what we need to do.

These kind of logics we can achieve in our program using if statements. It can take many forms which are described in detail in this chapter.

1. Simple if statement

Syntax

If(condition)
{
Statements which will be executed if the condition is true
}
Statements that need to be executed always

Here if the condition is true, the code which is written inside the curly brackets {} of the if block will be executed.

Example

We can take a look on a program which will display a message “Success” if a particular value is greater than 5.  It then displays a message “Executed successfully” and complete its execution.

Program

   
/*
 * FileName :  SimpleIfStatementDemo1.java
 */
public class SimpleIfStatementDemo1
{
     public static void main(String args[])
     {
    	 //Declaring a variable "test" and initializing it with a value 10
    	 int test=10;

    	 //Checking if "test" is greater than 5
    	 if(test>5)
    	 {
    		 //This block will be executed only if "test" is greater than 5
    		 System.out.println("Success");
    	 }

    	 //The if block ends.
    	 System.out.println("Executed successfully");
     }
}

Output

Program Output

This program outputs the message “Success” because the declared variable “test” has a value 10 which is greater than 5.

2. if else statement

Syntax


If(condition)
{
Statements which will be executed if the condition is true
}
else
{
Statements which will be executed if the condition is false
}
Statements that need to be executed always

Here if the condition is true, the code which is written inside the curly brackets {} of the if block will be executed.  If the condition is false, the code which is written inside the curly brackets {} of the else block will be executed.

Example

We can take a look on a program which will display a message “Success” if a particular value is greater than 5.   If this value is not greater than 5, it will display a message “Failure”. It then displays a message “Executed successfully” and complete its execution.

Program

  
/*
 * FileName :  IfElseStatementDemo1.java
 */
public class IfElseStatementDemo1
{
     public static void main(String args[])
     {
    	 //Declaring a variable "test" and initializing it with a value 10
    	 int test=10;

    	 //Checking if "test" is greater than 5
    	 if(test>5)
    	 {
    		 //This block will be executed only if "test" is greater than 5
    		 System.out.println("Success");
    	 }
    	 else
    	 {
       		 //This block will be executed only if "test" is not greater than 5
    		 System.out.println("Failure");

    	 }

    	 //The if else blocks ends.
    	 System.out.println("Executed successfully");
     }
}

Output

Program Output

This program outputs the message “Success” because the declared variable “test” has a value 10 which is greater than 5.

If the value of “test” was lesser than or equal to 5, the message “Failure” would have been displayed instead of “Success”. 

Please change the value of the variable “test” and execute the program and observer different results.

3. if–else-if ladder

Syntax

If(condition-1)
{
Statements which will be executed if condition-1 is true
}
else if (condition-2)
{
Statements which will be executed if condition-2 is true
}
.
.
.
else if (condition-n)
{
Statements which will be executed if condition-n is true
}
else
{
Statements which will be executed if none of the conditions in
condition-1, condition2,…condition-n are true.

}
Statements which will be executed always

As you can see in this if-else-if ladder, conditions are evaluated from top. First condition-1 will be evaluated and if this is true, the code inside the if block will be executed. If condition-1 is false, condition-2 will be evaluated.

If condition-2 is true, the code inside that else if block will be executed. If condition-2 is false, condition-3 will be evaluated. This will go on like this.

If none of the conditions are true, the code inside the else block will be executed.

Example

We can take a look on a program which will

1. display a message “Hello” if a particular value is equal to 1.  
2. display a message “Hi” if a particular value is equal to 2.  
3. display a message “Good” if a particular value is equal to 3.  
4. display a message “No Match Found”  if none of the above conditions are matching.

Program

/*
 * FileName :  IfElseIfLadderDemo1.java
 */
public class IfElseIfLadderDemo1
{
     public static void main(String args[])
     {
    	 //Declaring a variable "test" and initializing it with a value 2
    	 int test=2;

       	 if(test==1)
    	 {
    		 //This block will be executed only if "test" is equal to 1
    		 System.out.println("Hello");
    	 }
    	 else if(test==2)
    	 {
       		 //This block will be executed only if "test" is equal to 2
    		 System.out.println("Hi");

    	 }
    	 else if(test==3)
    	 {
       		 //This block will be executed only if "test" is equal to 3
    		 System.out.println("Good");
    	 }
    	 else
    	 {
    		 System.out.println("No Match Found");
    	 }
     }
}

Output

Program Output

This program outputs the message “Hi” because the declared variable “test” has a value 2.

If the value of “test” was 1, the message “Hello” would have been displayed.

If the value of “test” was 3, the message “Good” would have been displayed

If the value of “test” was any other than 1, 2, 3, the message “No Match Found” would have been displayed

Please change the value of the variable “test” accordingly and execute the program and see the different outputs.

4. Nested if…else statement

You can combine multiple if / if-else /if-else-if ladders when a series of decisions are involved.  So you can make sure that your program executes certain instructions when a series of conditions are met.

Example

The following program outputs the message

  1. “Hi, test1 is 5 and test2 is 3” if  test1 is equal to 5 and test2 is equal to 3
  2. “Hi, test1 is 5 and test2 is some value other than 3” if test1 is equal to 5 and test2 is some value other than 3
  3. “Hi, test1 is 4” if test1 is 4
  4. “Hi, test1 is 3 and test2 is 3” if test1 is equal to 3 and test2 is equal to 3
  5. “Hi, test1 is 3 and test2 is 2” if test1 is equal to 3 and test2 is equal to 2
  6. “Hi, test1 is some value other than 5,4,3” if test1  is some value other than 5,4,3

Program

/*
 * FileName :  NestedDemo1.java
 *
 */
public class NestedDemo1
{
	public static void main(String args[])
	{
		//Declaring a variable test1 and initializing it with a value 5
		int test1=3;
		//Declaring a variable test2 and initializing it with a value 3
		int test2=3;

		if(test1==5)
		{
			//This block will be executed only if "test1" is equal to 5
			if(test2==3)
			{
				/*This block will be executed only if
				 test1 is equal to 5	and test2 is equal to 3 */
				System.out.println("Hi, test1 is 5 and test2 is 3");

			}
			else
			{
				/*This block will be executed only if
				 test1 is equal to 5 and test2 is some value other than 3 */
				System.out.println("Hi, test1 is 5 and test2 is some value other than 3");
			}
		}
		else if(test1==4)
		{
			//This block will be executed only if test1 is 4
			System.out.println("Hi, test1 is 4");

		}
		else if(test1==3)
		{
			//This block will be executed only if "test1" is 3

			if(test2==3)
			{
				/*This block will be executed only if
				 test1 is equal to 3 and test2 is equal to 3 */
				System.out.println("Hi, test1 is 3 and test2 is 3");

			}
			else if(test2==2)
			{
				/*This block will be executed only if
				 test1 is equal to 3 and test2 is equal to 2 */
				System.out.println("Hi, test1 is 3 and test2 is 2");
			}

		}
		else
		{
			/*This block will be executed only if
			 test1  is some value other than 5,4,3*/
			System.out.println("Hi, test1 is some value other than 5,4,3");
		}
	}
}

Output

Program Output

This program outputs the message “test1 is 3 and test2 is 3” because variable“test1” is 3 and  “test2” is also 3.

Please change the value of the variables “test1” and “test2” and execute the program and observer the different results.


Comments(12)


Anandharaja 25 Jul 2014 11:53 AM
download very useful
Like (0)| Dislike (0)| Reply| Flag

Atul sharma 21 Jun 2014 9:13 AM
I think this is the most easy online tutorial i have found on web! 
Like (0)| Dislike (0)| Reply| Flag

anusha 27 May 2014 3:54 AM
easy to understand, well explained. :)

a suggestion: you can do similar work in web development. it will be very useful to learn the basics easily.

thank you!
Like (0)| Dislike (0)| Reply| Flag

akash 18 Apr 2014 6:52 AM

very good ....

Like (0)| Dislike (0)| Reply| Flag

Anshuman 12 Apr 2014 4:11 PM
Very well explained, in simple yet proper way.


Like (0)| Dislike (0)| Reply| Flag

Priya 06 Apr 2014 2:25 AM
Good ,easy to learn and understand
Like (0)| Dislike (0)| Reply| Flag

DisiDawn 17 Dec 2013 4:16 PM
what about this original code and modified code? what should I place there?

Like (0)| Dislike (0)| Reply| Flag

prakash 22 Jul 2013 1:01 AM
download this topic
Like (0)| Dislike (0)| Reply| Flag

surendar 21 Jul 2013 9:09 AM
good.
Like (0)| Dislike (0)| Reply| Flag

saujanya 06 Jun 2013 3:32 AM
you could elaborate more in this web page. but it\'s nice.
Like (0)| Dislike (0)| Reply| Flag

next last
Add a new comment ...


Name   Email

Please answer the simple math question given below

10 + 9 =


close

Sign in