« Previous Page Next Page »
    

Introduction to Decision Making in Java.

POSTED

AUTHOR: VARUN SINGH

In this tutorial, you are going to learn Java Decision Making like If Statement, If-else Statement, Nested-if Statement, If-else-if Statement, Switch Case Statement with their Java Syntax.

A programming language uses control statements to control the flow of execution of program based on certain conditions. Java has such decision-making capabilities within its program by the use of following the decision making statements:


  • 1. If
  • 2. If-else
  • 3. Nested-if
  • 4. If-else-if
  • 5. Switch-case
  • 6. Jump

  • 1. Java If Statement:

    if statement is the most simple decision making statement use to tests the condition. It is used to check whether a block of statements will be executed or not. If condition is true then a block of statement is executed otherwise not.

    Syntax: java if statement:

    if(condition)
         {
               // Statements to execute if condition is true
         }
    

    Example: if condition is true

    class IfDemo
      {
       public static void main(String args[])
         {
               int i = 15;
               if (i < 20)
               {
                 System.out.println("15 is less than 20");   
               }
               
          }
       }
    

    Output:

    10 is less than 15
    


    2. Java if-else Statement:

    The if statement is used when a condition is true then it will execute a block of statements. But what if you want to do something else if your condition is false. Here you can use if-else statement to execute a block of code when the condition is false.

    Syntax: java if-else statement:

    if (condition)
      {
        // Executes this block if condition is true
      }
    else
      {
         // Executes this block if condition is false
      }
    

    Example:

    // Java program to illustrate if-else statement
    class IfElseDemo
      {
        public static void main(String args[])
        {
           int i = 20;
            
           if (i < 25)
           {
             System.out.println("i is smaller than 25"); 
           }
         
           else
           {
             System.out.println("i is greater than 25");
           }
            
        }
      }
    

    Output:

    i is smaller than 25
    



    3. Java Nested-If Statement:

    A nested if is an if statement that is used inside another if statement. If a if statement target another if statement is called nested-if statement. Java allows us to nest if statements within if statements.

    Syntax: java nested-if statement:

    if (condition1) 
    {
       // Executes when condition1 is true
       if (condition2) 
       {
          // Executes when condition2 is true
       }
    }
    

    Example:

    // Java program to illustrate nested-if statement
    class NestedIfDemo
    {
        public static void main(String args[])
        {
            int i = 10;
     
            if (i < 10)
            {
                // First if statement
                if (i < 6)
                {
                    
                    System.out.println("i is smaller than 6");
                }
                
                else
                {
                    System.out.println("i is greater than 6");
                }
            }
        }
    }
    

    Output:

    i is smaller than 6
    



    4. Java if-else-if Statement:

    In java if-else-if ladder, the user can use many if else statement within the loop. If one condition holds true the rest of the loops is bypassed.

    Syntax: java nested-if statement:

    if(condition1){  
    //code to be executed if condition1 is true  
    }else if(condition2){  
    //code to be executed if condition2 is true  
    }  
    else if(condition3){  
    //code to be executed if condition3 is true  
    }  
    ...  
    else{  
    //code to be executed if all the conditions are false  
    }
    

    Example:

    // Java program to illustrate if-else-if 
    class IfElseIfDemo
      {
        public static void main(String args[])
        {
          int i = 20;
          if (i == 5)
          System.out.println("i is 5");
          else if (i == 10)
          System.out.println("i is 10");
          else if (i == 20)
          System.out.println("i is 20");
        else
        System.out.println("i is not present");
        }
      }
    

    Output:

    i is 20
    



    5. Java Switch Case Statement:

    Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

    Syntax: java switch-case statement:

    switch(expression){    
    case value1:    
     //code to be executed;    
     break;  //optional  
    case value2:    
     //code to be executed;    
     break;  //optional  
    ......    
        
    default:     
     code to be executed if all cases are not matched;    
    }
    

    Example:

    public class SwitchDemo {
        public static void main(String[] args) {
    
            int month = 8;
            String month;
            switch (month) {
                case 1:  
                month = "January";
                break;
                case 2:  
                month = "February";
                break;
                case 3:  
                month = "March";
                break;
                case 4:  
                month = "April";
                break;
                case 5:  
                month = "May";
                break;
                case 6:  
                month = "June";
                break;
                case 7:  
                month = "July";
                break;
                case 8:  
                month = "August";
                break;
                case 9:  
                month = "September";
                break;
                case 10: 
                month = "October";
                break;
                case 11: 
                month = "November";
                break;
                case 12: 
                month = "December";
                break;
                default: 
                month = "Invalid month";
                break;
            }
            System.out.println(month);
        }
    }
    

    Output:

    August
    


    6. Jump Statement in Java

    Java has three jump statements; they are:

  • 1. Break
  • 2. Continue
  • 3. Return
  • 1. Break Statement:

    Java break statement used to terminate the program. Using break, we can force immediate termination of a loop, bypassing the conditional expression before break statements in the body of the loop.

    Example:

    // Java program to illustrate using break to exit a loop
    
    class BreakLoopDemo
    {
        public static void main(String args[])
        {
            // Initially loop is set to run from 0-9
            for (int i = 0; i < 10; i++)
            {
                // terminate loop when i is 4.
                if (i == 4)
                    break;
     
                System.out.println("i: " + i);
            }
            System.out.println("Loop complete.");
        }
    }
    

    Output:

    i: 0
    i: 1
    i: 2
    i: 3
    Loop complete.
    

    2. Continue Statement:

    This statement is used to continue an iteration of a loop, i.e. to continue for a loop to run with a different condition.

    Example:

    // Java program to illustrate using continue in an if statement
    
    class ContinueDemo
    {
        public static void main(String args[])
        {
            for (int i = 0; i < 10; i++)
            {
                // If the number is even
                // skip and continue
                if (i%2 == 0)
                    continue;
     
                // If number is odd, print it
                System.out.print(i + " ");
            }
        }
    }
    

    Output:

    1 3 5 7 9 
    

    3. Return Statement:

    The return statement is used to explicitly return from a method. That is, it causes a program control to transfer back to the caller of the method.

    Example:

    // Java program to illustrate using return
    class ReturnDemo
    {
        public static void main(String args[])
        {
            boolean t = true;
            System.out.println("Before the return.");
         
            if (t)
                return;
     
            // Compiler will bypass every statement after return
            
            System.out.println("This won't execute.");
        }
    }
    

    Output:

    Before the return.
    


    The End:

    Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

    If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

    « Previous Page Next Page »

    Comments: