Java - Decision Making

If statement:

 

The if statement is Java’s conditional branch statement.

Syntax:


If(Boolean expression){
// Code here
}

Example:


public class Main{
	public static void main(String args[]){
		int a = 10;
		int b = 20;
		if(a>b){
			System.out.println("a is greater than b");
		}
		if(b<a){
			System.out.println("b is less than a");
		}
}
}
		

Output:

a is greater than b

b is less than a

 

if else :

Syntax:


if(Boolean condition){
	// statement1
}else{
	// statement2
}


This if else work like : if condition is true than statement1 is executed otherwise statement2 is executed.

 

Example:


public class Main{
	public static void main(String args[]){
		int a = 10;
		int b = 20;
		if(a>b){
			System.out.println(?a is greater than b?);
                }else{
	                System.out.println(?b is greater than a?);
                }
	}
}


 

Output:

b is greater than a

 

If-else-if-ladder:

Syntax:


if(Boolean condition){
	// statement1
}else if(Boolean condition){
	// statement2
}else if(Boolean condition){
	// statement3
}
........
else{
	// else statement
}


 

The if statement executed from top to down. As soon as one of the condition is true, statement associated with that if statement executed.

 

If none of the condition is true than else statement will be executed, only one of the statement executed from list of else if statements.

 

Example:


public class Main{
	public static void main(String args[]){
		int percentage = 65;
		if(percentage >= 70){
			System.out.println(?First class with Distinction?);
		}else If(percentage >= 60){
			System.out.println(?First Class?);
		}else If(percentage >= 48){
			System.out.println(?Second Class?);
		}else If(percentage >= 36){
			System.out.println(?Pass Class?);
		}else{
			System.out.println(?Fail?);
                }

	}
}


 

Output:

First Class

switch statement:

The switch statement is multi way branch statement in java programming. It is use to replace multilevel if-else-if statement.

 

Syntax:


switch(expression){
case value 1:
	// statement 1
	break;
case value 2:
	// statement 2
	break;
case value n:
	// statement n
	break;
default:
	//statements
	break;
}


 

The expression type must be the byte, short, int and char.

Each case value must be a unique literal(constant not a variable). Duplicate case value not allowed.

The each case value compare with expression if match found than corresponding statement will be executed. If no match is found than default statement will be executed. Default case if optional.

The break statement use to terminate statement sequence, if break statement is not written than all statement execute after match statement.

 

Example:


public class Main{
	public static void main(String args[]){
 		int day = 1;
 		switch(day){
 			case 0:
				System.out.println("Sunday");
				 break;
 			case 1:
 				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("Invalid Day");
				break;
		}
	}
}


 

Output:

Monday


Note : switch statement support string from Java 7, means use string object in the switch expression.