In this particular article we will see about decision making(if-else and switch) in Java, there are two ways of decision making in general.

1) If-else statements

2) Switch cases statements

If-else statements in Java

If-else statements are most commonly used decision making keywords in any programming language and hence in Java. If-else statements works on the basis of a conditional expression in if(condition_expression), if the condition returns true than the code in if?s body executes and if the expression returns false than code in else?s body executes. Here is a basic syntax of simple if-else statement.

if(CONDITION_EXPRESSION){
		// executes if condition is true
	}
	else{
		//executes if condition is false
	}
The conditional expression in if?s body should return a boolean value, if not we will get a syntax error. Here are few example of a basic if-else statements.

Simple if statement in Java

We can use a if() without having a else associated with it, in this case if the CONDITION_EXPRESSION returns true, the body of if statement executes or program control moves to the next line after if?s body.

package com.beingjavaguys.example;

public class Test {
	public static void main(String args[]) {
		int number = 5;
		if (number >= 5) {
			System.out.println("Number is either equals to or greater than 5");
		}
	}
}

Output

Number is less than 5


Simple if-else statement in Java

We can have an optional else with a if statement, in this case if the CONDITION_EXPRESSION returns true, the body of if statement executes or the body of else?s executes.

package com.beingjavaguys.example;

public class Test {
	public static void main(String args[]) {
		int number = 5;
		if (number >= 6) {
			System.out.println("Number is either equals to or greater than 5");
		} else {
			System.out.println("Number is less than 5");
		}
	}
}

Output

Number is less than 5


Multiple if-else statement in Java

In Java we can have a combination of if-else-if as per the requirement, here is an simple example which have a if-else-if heirarchy

package com.beingjavaguys.example;

public class Test {
	public static void main(String args[]) {
		int number = 5;
		if (number > 5) {
			System.out.println("Number is greater than 5");
		} else if (number < 5) {
			System.out.println("Number is less than 5");
		} else {
			System.out.println("Number is equals to 5");
		}
	}
}

Output

Number is less than 5


Nested if else statements

We can have nested if-else-if as well, nested means a if-else combination inside the body of either a if or else or both, here is an simple example which explains the working of nested if-else-if.

package com.beingjavaguys.example;

public class Test {
	public static void main(String args[]) {
		int number = 5;
		if (number > 5) {
			if (number > 7) {
				System.out.println("Number is greater than 5 and 7");
			} else if (number == 7) {
				System.out.println("Number is greater than 5 and equals to 7");
			} else if (number < 7) {
				System.out.println("Number is greater than 5 but less than 7");
			}

		} else if (number < 5) {
			System.out.println("Number is less than 5");
		} else {
			System.out.println("Number is equals to 5");
		}
	}
}

Output

Number is less than 5


Here are few points to remember while using if-else-if stetemnts combination.

1) We can have a if with an else or without else.

2) An else can not be used withour an if, and else should be the very next statement after if?'s body.

The swithch statement in Java

A switch statement consists of a swith and a number of case statement, there can be a single case of a switch or multiple cases. The switch statement takes an expression to evaluate and all the cases are tested against the value of switch?s expressions. Any case value that is equals to the switch?s expression will be executed. If none of the case matches that we can a conditional default statement executes. Here is a simple example of switch in Java.

switch(conditionl_expression){
	    case value :
	       //Statements
	       break; //optional
	    case value :
	       //Statements
	       break; //optional
	// any number if 
	    default : //Optional
	       //Statements
	}
Here are certain rules that applies to an switch statement in Java.

1)  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

2)  The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.

3)  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

4)  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

5)  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

6)  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true.

Example of switch statement in Java


package com.beingjavaguys.example;

public class Test {
	public static void main(String args[]) {

		String gender = "Male";
		switch (gender) {
		case "Male":
			System.out.println("Student is a Male");
			break;
		case "Female":
			System.out.println("Student is a Female");
			break;
		default:
			System.out.println("Student gender not known");
		}
	}
}

Output

Number is less than 5


In this particular article we came across decision making in Java using if-else-if and switch statements, in upcoming articles we will see more about Core Java and related technologies.
  • By Techburps.com
  • Mar 1, 2015
  • Core Java