Search Placementyogi.com
 

Decision Control in Java

The common decision control in java are:

1. If Else control

2. Nested If 

3. If Else If Ladder

Lets discuss each of these in detail with some real world Examples!

If Else Control Statement in Java

Syntax:

if(condition)
{
	----;
	statements set1; // Compound statements
	----;
}
else
{
	----;
	statements set2;
	----;
}

If the condition Evaluates to True the set1 statements are EXECUTED  and set2 are NOT executed.

and if the statement is False, the statements set1 are NOT executed and set2 inside the else block are EXECUTED.

Single Statement:

If there is only one statement inside the if or else block the braces "{ }" can be removed.

Syntax: 

if (condition) 
	statement;
else:
	statement2;

Note 1: The Condition has to be a boolean Expression. And Java is very strict about it, If you pass any other type of expression which evaluates to a non boolean value, the compiler is gonna complain you right away! (Compile Time Error)

Note 2: Multiple Statements are technically called Compound Statements.

Example of If in Java:

class IfExample 
{
	public static void main(String[] args) 
	{
		int var1=0;
		if (var1 < 10)
		{
			System.out.println("Inside If Condition");
			System.out.println("2nd statement inside If Condition");
		}
		else
		{
			System.out.println("Inside Else Condition");
			System.out.println("2nd statement inside Else Condition");
		}
		
	}
}

For Single Statement the If block could be written as:

if (var1 < 10)
   	System.out.println("Inside If Condition");
else
	System.out.println("Inside Else Condition");

Be careful with these single statements:

int var1=10;
if (var1 != 10)
	System.out.println("Statement1");
	System.out.println("Statement2 Not under if block");

Here only "statement1" is under if block,  the Statement2 is not under if block and is executed irrespective of the result of the condition var1!=10.

Nested If in Java

A nested if means placing one or more if statements inside another if statement.

Syntax of Nested If in Java

if(condition1)
{
	//statementset1
	if(condition2)
	{	
		//statementset2
	}	
	else // executed if condition3 is false
	{
		//statementset3
	}
}
else     // executed if condition1 is false
{
	// statementset4
}

If Condition1 is true execute statementset1 and then check if condition2 is true if yes execute statementset2 also, else execute statementset3.

If Condition1 is false execute statementset4.

Example of Nested If in Java

int i=10,j=20;
if(i==10)
{
	if(j==20)
	{
		System.out.println("i=10 and y=20");
	}
}

i=10 and y=20 is printed only if both the if condition evaluates to true.

if-else-if Ladder in Java

This decision control structure is used when you have multiple conditions and you have to execute the corresponding statements based on which condition is true.

Syntax:

if(boolean_expression1)
{
	//... StatementSet1
}
else if (boolean_expression2)
{
	// StatementSet2
}
else if (boolean_expression3)
{
	// StatementSet3
}
// any number of else if
else
{
	//Last StatementSet
}

Example:

int a =10;
if( a>10)
	System.out.println("a>10");
else if( a>20)
	System.out.println("a>20");
else if(a<10)
	System.out.println("a<10");
else 
	System.out.println("No condition matched!");

Real world Example: 

Lets say you are alone at your home and you feel hungry, you go to the kitchen to see if there is anything available to eat.

Here is the thought process:

if( there is bread and butter)
{
	spread the butter on bread 
	eat the bread
}
else if ( there is oats and milk)
{
	eat oats and milk
}
else if ( there is maggie )
	make maggie and eat it

The conditions are written in the preference order and if any condition matches the rest of the ladder is skipped!

If you find bread and butter, your search just ends! you do not go and search for oats or maggie after that.

Banner to LogicGuns.com

Hide Page Information

If Else Decision Statement in Java with syntax and example

This tutorial will help freshers to understand the meaning of if-else in java with real world example and explanation along with programs that will help beginners in java to grasp the concept of if-else in java