Decision Making

Decision making statements in Java are structures which make the compiler to jump certain section of code based on the statements given inside. There are two decision making statements: => if statements => switch statements

If statements:

Syntax:

if(Boolean_expression)
{
   //Statements will execute if the Boolean expression is true
}
If the boolean expression given along is true, when statement inside the scope will be executed or else the compiler will jump the whole section and go to next piece of code.
If statements are of multiple types, namely:

If statements:

These are normal if statements, execution based on boolean_expression.
if(Boolean_expression)
{
   //Statements will execute if the Boolean expression is true
}

If else statements:

These are if statements in which if the boolean expression is false, the code inside else statement is executed.
if(Boolean_expression){
   //Executes when the Boolean expression is true
}else{
   //Executes when the Boolean expression is false
}

If - else if statements:

These are if statements where multiple values of boolean_expressions are provided and code section is executed wherever the value is true.
if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
}else {
   //Executes when the none of the above condition is true.
}

Nested if statements:

These are if statements inside if statements, that is one boolean_expression value being true leads to checking of another boolean_expression in the same manner.
if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
      //Executes when the Boolean expression 2 is true
   }
}

Examples on various if statements:

Examples 1:

Below is an example of using simple if-else condition.

public class IfDemo {
	public static void main(String[] args) {
		boolean gender = true;
		if(gender == true){
			System.out.println("male");
		}else{
			System.out.println("female");
		}
          }
}
Examples 2:

Below is an example of using simple if-else condition.

public class IfElseIfDemo {
	public static void main(String[] args) {
		String player = "Messi";
		if(player == "Ronaldo"){
			System.out.println("ronaldo plays for Portugal");
		}                          
                else if(player == "Messi"){
			System.out.println("Messi plays for Argentina");
	        }    
                else if(player == "Sachin"){
			System.out.println("Sachin plays for India");
                }
	}
}
Examples 3:

Below is an example of using simple nested if condition.

public class NestedIfDemo {
	public static void main(String[] args) {
		String player = "Messi";
		String team = "Argentina";
		if(player == "Messi"){
			if(team == "Argentina"){
				System.out.println("Messi plays for Argentina");
			}else{
				System.out.println("Messi doesn't play for Argentina");
			}
		}
	}
}
Example 4:

Below is an example of finding biggest of 3 numbers by using nested if, and if-else condition.

.
 
public class FindBigNumber {
	public static void main(String[] args) {
		//Finding biggest of 3 numbers
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter three 
                                    numbers");
		int i = sc.nextInt(); //read first value 
		int j = sc.nextInt(); //read the second value
		int k = sc.nextInt(); //read the third value
		
		System.out.println("biggest of 3 numbers is..");
		
		if(i>j){
			if(i>k){ //this is nested if
				System.out.println(i+"..is bigger");
			}else{
				System.out.println(k+"..is bigger");
			}
		}else if(j>k){ //this is else-if
			System.out.println(j+"..is greater");
		}else{
			System.out.println(k+"..is greater");
		}
		
	}
}
Example 5:

Write a program to check if a number is even or odd ?

 
public class CheckEvenOdd{
	public static void main(String[] args) {
		System.out.println("Enter the number which you want to check");
		Scanner scan=new Scanner(System.in);
		int num=Integer.parseInt(scan.next());
		if(num%2==0)
                       	System.out.println("Number is even: "+num);
		else
			System.out.println("Number is odd: "+num);
	}
}
Example 6:

Write a program to calculate factorial of an integer number ?

 
public class Factorial {
      public static void main(String[] args) {
		int no,fact=1;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter a No.");
		no=scan.nextInt();
		for(int i=1;i<=no;i++)
		{
			fact=fact*i;
		}
		System.out.println(fact);
	}
}

Switch statements:

Syntax

switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}
The switch statement executes the particular section of the code depending upon the variable value. As the value matches to some case, the code under that will be executed leaving out the rest.

Examples on switch-case statements:

Example 1:

Below example will read a character from keyboard, and it will display if it is 'a' or 'b' or none.

 
public class First {
    public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter a character");
		char ch = (char) System.in.read();
		switch(ch){
		case 'a':
			System.out.println("its a");
			break;
		case 'b':
			System.out.println("its b");
			break;
		default:
		                System.out.println("none");
		}
        }
}
Note:Try to convert all if-else if statements to switch case statements as much as possible, because switch case statements are executed faster than if-else conditions. Use as much less if-else conditions as possible to avoid programme clutter and slowness.