Java Decision Making

Decision making statements is also known as branching statements or conditional control statements. In real life, we make certain decisions and for those decisions, we will get a certain result. In programming there will be some conditions, if the condition becomes true then the block of code will be execute.

Java Decision Making
Java Decision Making

Decision making statements is also known as branching statements or conditional control statements.

In real life, we make certain decisions and for those decisions, we will get a certain result. In programming there will be some conditions, if the condition becomes true then the block of code will be execute.

A decision making statement contains one or more conditions to be evaluated along with a statement or statements that are to be executed if the condition is becomes true, and if the condition becomes false the other state will be executed, that is, the else statement will executed.

Conditional Statements

  • if
  • if-else
  • nested-if
  • switch case

if statement

If statement is one of the simple decision making statement. It is used to decide whether a certain block of statement will execute only, if the condition becomes true.

Syntax

if(condition){
 //statement to be executed if the condition becomes true
}

Example

class example
{
public static void main(String args[])
{
int a = 10;
if(a>5)
{
System.out.println("This is an example for simple if statement");
}
}
}

Output

This is an example for simple if statement

if else statement

In if else statement, if the condition becomes true the block of code inside the if statement will be executed, if the condition becomes false by default the else statement will be executed.

Syntax

if(condition)
{
 //statement to be executed if the condition becomes true
}
else
{
 //statement to be executed if the condition becomes false
}

Example

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

Output

B is greater than A

nested if statement

When an if statement contains another if statement, then it is called as nested if statement.

Syntax

if (condition_1)
{
 // statement to be executed if the condition_1 becomes true
 if (condition_2)
 {
 // statement to be executed if the condition_2 becomes true
 }
}
else
{
 //statement to be executed if the condition becomes false
}

Example

class example
{
public static void main(String args[])
{
int age;
age = 21;
if(age < 18)
{
System.out.println("You are Minor and you are not eligible to work");
}
else
{
if(age >= 18 && age <= 60)
{
System.out.println("You are Eligible to Work");
}
else
{
System.out.println("You are too old to work as per the Government rules");
}
}
}
}

Output

You are Eligible to Work

switch case statement

When there are multiple cases available in a program, switch statement is used. Switch statement is also called as multiway branch statement. It allows the user to select a particular case and will execute only the particular block of code. The break statement is used inside the switch statement to terminate a statement sequence. If the user select other than the given cases, the default block will execute and default statement is optional.

Syntax

switch(expression)
{
   case value :
      // Statements to be executed
      break;
   case value :
      // Statements to be executed
      break;
   case value :
      // Statements to be executed
      break;
   default : // Optional
      // Statements to be executed
}

Example

class example
{
public static void main(String args[])
{
   {
        int i = 1;
        switch (i)
        {
        case 0:
            System.out.println("Excellent");
            break;
        case 1:
            System.out.println("Well done");
            break;
        case 2:
            System.out.println("You passed");
            break;
        case 3:
            System.out.println("you failed, better luck next time");
            break;
        default:
            System.out.println("Invalid number");
          }
      }
}
}

Output

Well done

Jump statements

Jump statements are used to transfer control from one part of the program to other part of the program. There are three jump statements in java.

break keyword

Break is used to force termination of a loop, bypassing the remaining code of the loop body.

Example

class example
{
public static void main(String args[])
{
 int a = 20;
for(a = 1; a < 10; a++)
{
if(a==10)
break;
}
System.out.println("This is an example for break keyword");
}
}

Output

This is an example for break statement

continue keyword

The continue statement immediately jump to the next iteration of loop and execute other statement.

Example

class example
{
public static void main(String args[])
{
 int a = 20;
for(a = 1; a < 10; a++)
{
if(a==10)
continue;
System.out.println(a);
}
System.out.println("This is an example for continue statement");
}
}

Output

1
2
3
4
5
6
7
8
9
This is an example for continue statement

return keyword

The return statement is used to return from a method. return type may be a void or primitive. Void type returns nothing and primitive type returns integer, float, double, etc.

Example

class example
{
public static void main(String args[])
{
int a = 20;
System.out.println("Before return");
{
if(a==20)
return;
}
System.out.println("After return");
}
}

Output

Before return