Sunday 5 June 2016

Decision making in Java

In this blog post we will learn about how to use decision making statement in java program.Decision making structures have one or more conditions to be validated or tested by the program.If the condition is to be true (true block statement will be executed) and if the condition is to be false (false block statement will be executed).



Fig:Decision Making in java












Java provides following types of decision making statements.

1-if statement
An if statement consists of a Boolean expression or test condition followed by one or group of statements.If condition is

true then the code between if statements are executed.The syntax of if statement is:

Syntax:
The syntax of an if statement is:

if(Boolean expression or Test Condition)
{
   //Statements will execute if the Boolean expression is true
}


Demo Example:
public class DemoIf {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("Print if statement");
      }
   }
}

O/P Result:

Print if statement


2-if else statement

An if ..else  statement consists of a Boolean expression or test condition followed by one or group of statements.If

condition is true then the code between if statements are executed.If condition is false then the statements between else

block are executed.


Syntax:

The syntax of an if...else is:

if(Boolean expression or Test Condition){
   //Executes if block statements when the test condition is false
}
else{
   //Executes else block statements when the test condition is false
}


Example:
public class DemoIfElse {

   public static void main(String args[]){
      int x = 40;

      if( x < 10 ){
         System.out.print("The statement of if block executed");
      }else{
         System.out.print("The statement of else block executed");
      }
   }
}
O/P Result:

The statement of else block executed



2-The if...else if...else Statement:

An if ..else  statement consists of a Boolean expression or test condition followed by one or group of statements.If

condition is true then the code between if statements are executed.If condition is false then the statements between else

block are executed.The if...else if...else statement is used on those conditions when we perform multiple condition check.

Syntax:
The syntax of an if...elseif is:

if(Test Condition 1){
   //Executes when the Test Condition 1 is true
}else if(Test Condition 2){
   //Executes when the Test Condition 2 is true
}else if(Test Condition 3){
   //Executes when the Test Condition 3 is true
}else {
   //Executes when the none of the above condition is true.
}
Example:
public class DemoIfElseIf {

   public static void main(String args[]){
      int x = 40;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 40 ){
         System.out.print("Value of X is 40");
      }else{
         System.out.print("This is else statement");
      }
   }
}
O/P Result:

Value of X is 40



3-nested if statements

It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or

else if statement.

Syntax:
The syntax for a nested if...else is as follows:

if(Test Condition 1){
   //Executes when the Boolean expression 1 is true
   if(Test Condition 2){
      //Executes when the Boolean expression 2 is true
   }
}
You can nest else if...else in the similar way as we have nested if statement.

Demo Example:
public class DemoNestedIf {

   public static void main(String args[]){
      int x = 40;
      int y = 10;

      if( x == 40 ){
         if( y == 60 ){
             System.out.print("X = 40 and Y = 60");
          }
       }
    }
}
O/P Result:

X = 40 and Y = 60

4-switch statement

A switch statement allows a variable to be tested against a list of values.Each value in list is called a case, and the

variable being switched on is checked for each case.

Syntax:
The syntax of enhanced for loop is:

switch(Test expression){
    case value :
       //Statements
       break;                     //optional
    case value :
       //Statements
       break;                    //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}



Demo Example:
public class DemoSwitch {

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

      switch(grade)
      {
         case 'A' :
            System.out.println("Grade A");
            break;
         case 'B' :
         case 'C' :
            System.out.println("Grade B");
            break;
         case 'D' :
            System.out.println("Grade C");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Grade not awarded");
      }
      System.out.println("Your grade is "+ grade);
   }
}

O/P Result:

Your grade is a Grade B.

No comments:

Post a Comment