Java Beginner's Guide[6] - Decision making

Previous | Next


Hi, When you come with a situation where you need to make a decision, you think about the different solutions according to different conditions of that situation, in the real life. Aren't you? In programming also there are some special steps/ block of statements to do the decision making.

In programming, Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.


Java programming language provides following types of decision making statements. They are,

if statement - An if statement consists of a boolean expression followed by one or more statements.


Example: 

public class Test { 

   public static void main(String args[]) { 

        int x = 10; 
        if( x < 20 ) { 
           System.out.print("This is if statement"); 
        } 
   } 
}

Output: 

This is if statement



if...else statement - An if statement can be followed by an optional else statement, which executes when the boolean expression is false. 


Example:

public class Test { 

   public static void main(String args[]) {

        int x = 30; 
        if( x < 20 ) {  
             System.out.print("This is if statement"); 
        }else { 
             System.out.print("This is else statement"); 
        } 
   }  
}

Output:

This is else statement



nested if statement - You can use one if or else if statement inside another if or else if statement(s).

Example:

public class Test { 

       public static void main(String args[]) { 

               int x = 30; 
               int y = 10; 
               if( x == 30 ) { 
                       if( y == 10 ) { 
                                 System.out.print("X = 30 and Y = 10"); 
                       } 
               } 
        } 
}

Output:

X = 30 and Y =10


switch statement - A switch statement allows a variable to be tested for equality against a list of values.



The following rules apply to a switch statement.
  • The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums.
  • 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.
  • 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.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • 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.
  • 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. No break is needed in the default case.

Example:

public class Test { 

     public static void main(String args[]) { 
           
             char grade = 'C'; 

             switch(grade) { 

                  case 'A' : 
                          System.out.println("Excellent!");  
                      break; 
                  case 'B' : 
                  case 'C' : 
                          System.out.println("Well done"); 
                          break; 
                  case 'D' : 
                          System.out.println("You passed"); 
                  case 'F' : 
                          System.out.println("Better try again"); 
                          break; 
                  default : 
                          System.out.println("Invalid grade"); 
           } 
           
           System.out.println("Your grade is " + grade); 
      } 
}


Output:

Well done
Your grade is C


From next tutorial you will learn loops in Java. Stay tuned! Subscribe my blog.

And If you get any problem regarding to this tutorial, please email me or put a comment.

So, Let's meet again with the next tutorial. Till then Good Luck!.


References:
https://www.tutorialspoint.com/java


Previous | Next

Thank you!

Kalpani Ranasinghe
email: kalpanibhagya.kb@gmail.com
Facebook icon Twitter icon LinkedIn icon Google Plus icon

Comments