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(4)
Sign in (optional)
showing 1-4 of 4 comments,   sorted newest to the oldest

saikumar
2014-12-28 14:18:34 
int a=2,b=3;
if(a==3)
     if(b==3)   
           System.out.println("one");     
     else
           System.out.println("two");
      System.out.println("three");

-->Code doesn't have curly braces.
what is o/p & explain?
(0) (0) Reply
hansa
2014-12-15 19:22:06 
how many conditions we give in nested if else statement in java

(0) (0) Reply
Jiju
2011-03-01 16:34:32 
Hi Karthik, switch statements is explained in the next chapter.
(0) (0) Reply
Karthik S
2011-02-28 13:23:45 
You can also add the switch case construct here.
(0) (0) Reply
Add a new comment ...
Use Discussion Board for posting new aptitude questions.


Name   Email (optional)

6 + 5 = (please answer the simple math question)

Post Your Comment