Java Decision Making

You are free to check some expression to evaluates some block of code as per your requirement. Java provides decision making statements to perform this task.

Java Decision Making Statements

Java supports the following two decision making statements :

These statements allows you to control the flow of program's execution based upon the conditions known only during the run-time.

The if Statement

The if statement comprises of Boolean expression, followed by one or more statements. You will learn about the if statement in separate chapter.

The switch Statement

The switch statement allows a variable to be tested for equality against the list of values. Each value is called a case, and the variable which being switched on is checked for the each case. You will learn about switch statement in separate chapter.

Java Decision Making Program

Here is an example program, helps in understanding how the decision making statement works in Java:

/* Java Decision Making - Example Program */

public class JavaProgram
{   
    public static void main(String args[])
    {
        int num1=50, num2=60;
        if(num1>num2)
        {
            System.out.println("num1 is greater than num2");
        }
        else
        {
            System.out.println("num1 is not greater than num2");
        }
    }
}

Here is the output produced by the above Java program:

java decision making

Java Online Test


« Previous Tutorial Next Tutorial »