Sunday, June 12, 2016

Decision Making

Java provides two statements for decision making: if and switch. Both are used to control the statement execution depending on condition determined before.


if Statement


if statement can be used to handle the selection of branching which is based on one, two, or more than two conditions. Here is an explanation of if statement for each condition.



One Condition


The structure is the simplest because it only involves one condition. A common form of writing if statement with one condition as follows:

//If only consists of one statement
if(condition) statement

//If consists of two or more statements
if(condition) {
   statemen1;
   statemen2;
   ...
}

Program below will demonstrate the use of if for one condition:

class OneConditionIfDemo {

   public static void main(String[] args) {

      int a=1, b=10:

      if (a < 5) {
         System.out.println("Value of a is less than 5”);
      }

      if (b < 5) {
         System.out.println("Value of b is less than 5”);
      }
   }
}

The result will be given by the above program as follows:

Value of a is less than 5

From these results, it appears that only a program executed by the statement contained in the first if block. This is because the expression (a < 5) is true or fulfilled. In contrast, the second if block, expression (b < 5) is false so that the statement contained in the block are never executed by the program. Now, we take a second example, the condition will check the value of a character. Here is a code that can be written:

class OneConditionIfDemo2 {

   public static void main(String[] args) {

      char ch = 'E';

      if (ch == 'a' || ch == 'A’ ||
         ch == 'i' || ch == 'I' ||
         ch == 'u' || ch == 'U' ||
         ch == 'e' || ch == 'E' ||
         ch == 'o' || ch == 'O') {
         System.out.println(ch + " is vowel.");
      }
   }
}

When executed, the above program will give the following results:

E is vowel.



Two Conditions


The structure of this type is more complex compared with the first type above. In this form, the program provides an additional condition to handle events that are not met or the first is false. The general form of the electoral structure of this type as follows:

// If only consists of one statement
if (condition)
   statement if the condition is true
else
   statement if the condition is false

// If the statement consists of two or more
if (condition) {
   // Statements to be excuted if the condition is true
   statemen1;
   statemen2,
   ...
} else {
   // Statements to be excuted if the condition is false
   statemen1;
   statemen2;
   ...
}

Here is a sample program that demonstrates the use of statement using two conditions.

class TwoConditionIfDemo1 {

   public static void main(String[] args) {

      int a=1, b=10

      if (a < 5) {
         System.out.println(a + " is less than 5");
      } else {   //(a >= 5)
         System.out.println(a + " is greater than 5");
      }

      if (b < 5) {
        System.out.println(b + " is less than 5");
      } else {   //(b >= 5)
        System.out.println(b + " is greater than 5");
     }
   }
}


When executed, the above program will give the following results:

1 is less than 5
10 is greater than 5

It appears from the above results that at first if block, condition (a < 5) is true, then the statement contained in these conditions will be directly executed. The statement contained in the else part will be executed if the condition is false or not met. The second if condition (b < 5) is not met. the statement that will be executed is the statement contained in the else block. As another example, here we will modify the program in the previous section. In this program, we can determine whether we enter the letter which is vowel or consonant. Here is the code:

class TwoConditionIfDemo2 {

   public static void main(String[] args) {

      char ch = 'B';

      if (ch == 'a' || ch == 'A' ||
         ch == 'i' || ch == 'I' ||
         ch == 'u' || ch == 'U' ||
         ch == 'e' || ch == 'E' ||
         ch == 'o' || ch == 'O') {
         System.out.println(ch + " is vowel.");
      } else {
         System.out.println(ch + " is consonant.");
      }
   }
}


When executed, the above program will give the following results:

B is a consonant.

In the if block above, the condition is false so the program execute the else block.



Three or More Conditions


The selection structure of this type is an extension of previous types. Here, if statement has more than two conditions. The general form of writing as follows:

if (condition1)
   statement if the condition1 is true
else if (condition2)
   statement if the condition1 is false
else
   statement if condition1 and condition2 is false

Here is an example of a program that can show the workings of the electoral structure if that had three cases.

class ThreeConditionIfDemo1 {

   public static void main(String[] args) {

      int number = 4;

      if (number < 0) {
         System.out.println(number + " is NEGATIF number.");
      } else if (number == 0) {
         System.out.println("Value entered is ZERO”);
      } else { //(number > 0) 
         System.out.println(number + " is POSITIVE number.”);
      }
   }
}



When executed, the above program will give the following result:

4 is POSITIVE number.

Initially, the program will check whether a given number (in this case number = 4) is less than zero or not. Because the expression (4 < 0) is false, then the program will check the next else condition (4 == 0), and will not execute the statement, because the result is also false, then the program will execute the statement contained in the last else.

Now, we will make an example of another program which contains more than three conditions. Here, we will take the case which is the conversion of a number value to the index value (A, B, C, D, or E). For example, here we already have formula as follows:

value = (60% x last exam result) + (40% x mid exam result)

value >= 80: A
value < 80 and >= 70: B
value < 70 and >= 50: C
value < 50 and >= 30: D
value < 30: E

By using these conditions, then we can write the code as follows:

class Demo ThreeConditionIfDemo2 {

   public static void main(string[] args) {

      char indexValue;
      double midExamResult, lastExamResult, finalScore;

      midExamResult = 75.0;
      lastExamResult = 60.0;

      //Calculate final score using formula above
      finalScore = (0.4 * midExamResult) + (0.6 * lastExamResult);

      if (finalScore >= 80) {
         indexValue = 'A';
      } else if (finalScore >= 70) {
         indexValue = 'B';
      } else if (finalScore >= 50) {
         indexValue = 'C';
      } else if (finalScore >= 30) {
         indexValue = 'D';
      } else {   //(finalScore < 30)
         indexValue = 'E';
      }

      System.out.println("Final score: " + finalScore);
      System.out.println("Index value: " + indexValue);
   }
}


When executed, the program will provide the following result:

Final score: 66.0
Index value: C



switch Statement


The switch statement is an alternative to an decision making statement. This statement is usually used to simplify the complexity of the if statement that contain many conditions. Here is a common form of writing the syntax for the switch statement:

switch (expression) {
   case value1:
      // Statement will be executed when expression is same with value1
      break;
   case value2:
      // Statement will be executed when expression is same with value2
      break;
   ...
   case valueN :
      // Statement will be executed when expression is same with valueN
      break;
   default:
      // Statement will be executed when all defined values ​​are not same with the expression

Here is an example of a program that will show the use of the switch statement in Java:

class SwitchDemo1 {

   public static void main(String[] args) {

      int numberOfDay = 4;

      switch (numberOfDay) {
         case 1:
            System.out println("Day " + numberOfDay + " is Sunday");
            break;
         case 2:
            System.out println("Day " + numberOfDay + " is Monday");
            break;
         case 3:
            System.out println("Day " + numberOfDay + " is Tuesday");
            break;
         case 4:
            System.out println("Day " + numberOfDay + " is Wednesday");
            break;
         case 5:
            System.out println("Day " + numberOfDay + " is Thursday");
            break;
         case 6:
            System.out println("Day " + numberOfDay + " is Friday");
            break;
         case 7:
            System.out println("Day " + numberOfDay + " is Saturday");
            break;
         default:
            System.out.println("There is no day of " + numberOfDay);
      }
   }
}

The result will be given by the above program as follows:

Day 4 is Wednesday

As shown in the above result that the statement to be executed is a statement that is at a value which is equal to the expression defined. If all defined constant value not in accordance with the expression, then the progam will execute the statement contained in default section.

2 comments:

  1. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Gurgaon

    ReplyDelete