Java Chap6 Decision Making Statements ( Prof. Ananda M Ghosh.)

  • Uploaded by: Prof. (Dr) Ananda M Ghosh.
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Chap6 Decision Making Statements ( Prof. Ananda M Ghosh.) as PDF for free.

More details

  • Words: 1,661
  • Pages: 8
Chapter 6: Decision Making in Java Programs

6.0 Introduction When a computer program gets executed, its instructions are fetched normally in a sequential order unless decision-making statements are used to break that sequence. Thus a decision-making instruction can control the execution flow of a program causing a jump to any forward or backward point from its current location. Suppose there are 1000 instructions in a program and there is no use of decision making instructions. That particular program will start executing from the first instruction and will end up when the 1000th instruction gets finished. In reality, most programs are not so simple types. There are many problems, which demand alternative paths of instruction execution for reaching the desired state of solution. Alternate paths may be required to take care of different input data and situations arising out of condition tests performed for further actions to be taken to reach the final solution. Which alternative path is to be followed to reach the final solution is controlled by the decision making statement(s) used in a program. That is why decision-making statements are also called control statements. There are mainly three categories of control statements -selection, iteration and jump statements. We will talk about Java’s selection statements in this chapter. Discussions on iteration and jump statements will be made in the next chapter. 6.1 If-[then] & if-[then]-else Control Java has two main selection statements – if and switch. The general form of ifstatement is if ( condition ) statement1; else statement2; where statement1 and statement2 may be a single statement or a block enclosed within {...} brackets. The condition expression must return a boolean value like true or false.

The else part is optional. For example int x,y; | if (x < y) x =10; else y = 10;

// writing else explicitly is optional

If the condition (x < y) is true, then the statement x = 10; gets executed, else the statement y = 10; will be execute d. Thus a selection between two alternatives can be made in a program observing the status of the condition expression. The program control takes the form of – if ( condition) is true then action1 (using statement1) else take action2 (using statement2). Note: The word then is not written explicitly, but meant logically. The importance of else part can be understood from fig- 6.1(b). if

if

condition true

condition false

true

false

Statement1 Statement1

Statement2

Next Statement in Sequence Next Statement in Sequence

Fig –6.1 (a) if-then

(b) if-then-else

Fig-6.1 shows the flow control pattern of if-then and if-then-else constructs. Now an example can make the points clear. // Example 6.1 – if-else Demo public class IfDemo { public static void main(){ int a = 12, b = 9, c =18; int max;

if ( a > b) max = a; else max = b; if ( max < c) max = c; System.out.println(" The maximum value is = " + max); } } On execution, the output will appear on the terminal window as – The maximum value is = 18 6.1.1

Nested ifs Within one if-else statement another if-else statement can be enclosed if requirement demands that. Such inclusion is called nesting. Nested ifs are very common in programming. Let us now see an example of nesting. // Example 6.2 Nested if-then-else Demo public class NestedIfDemo { public static void main(){ int a = 10, b = 20, c = 8; int max = a; if ( max < b) { if ( b < c) max =c; else max = b; // nested portion } else max = a; // associated with outer if (max < b); System.out.println( " The maximum of three numbers is = " + max); } }

Just observe one aspect of the nested-if construct that else part of the first if-then is written outside the embedded (i.e. inner) if-else block { .....}. A sequence of nested ifs is possible and called as if-else-if Ladder which will look like this: if (condition1) statement; else if ( condition2) statement; else if (condition3) statement;

| | else statement; At present, there is no scope of further discussion on this subject here. So we leave it for you to learn when you become a matured programmer. An example of if-then-if Ladder will be shown now. // Example 6.3 If-Then-If Ladder Demo public class IfThenLadderDemo { public static void main() { // if purchase is <= Rs.5000, discount is 5% // if purchase is > 5000 -- but < 10000, discount is 10% // if purchase is >10000 but < 20000, discount is 15% // if purchase is > Rs. 20000, discount is 20% double purchase = 12780; System.out.println(" Purchase Value : " + purchase); double discount, bill_value; if (purchase <= 5000) { discount = .05 * purchase; bill_value = purchase - discount; System.out.println(" Bill amount will be : " + bill_value); } else if (purchase > 5000 && purchase <= 10000) { discount = .1 * purchase; bill_value = purchase - discount; System.out.println(" Bill amount will be : " + bill_value); } else if ( purchase >10000 && purchase <= 20000) { discount = .15 * purchase; bill_value = purchase -discount; System.out.println(" Bill amount will be : " + bill_value); } else if ( purchase > 20000) {

bill_value = .8 * purchase; System.out.println(" Bill amount will be : " + bill_value); } } } If you run this program, you will see the output as shown below (Picture 6.1)–

Picture 6.1 Since you have not learnt how to input data from a terminal, the program has not kept any scope for data inputting. To see the result with different gross purchase values, you have no other alternative but to change the purchase value in the program itself. Try it yourself to verify the discount calculations for different purchase values for different customers. 6.1.2 The Use of ?:

Operator

As mentioned in chapter-5, the ternary ?: operator ( also called conditional operator) can be used to replace certain types of if-then-else control statements. The general form of ?: operator is (boolean expression) ? expression2 : expression3; When boolean expression is true, expression2 is to be evaluated, else expression3 is evaluated. Let us see an example which will give an idea about the use of ?: operator. // Example-6.4 Demo of ?: operator use public class TernaryOpDemo { public static void main() { int a = 30, b = 54, c = 25; int d = (a > b) ? (a - 10) : (a + 15);

int e = ( b > c) ? (c + 15) : (c - 15); int f = (a < c) ? (a * 2) : ( a / 2); System.out.println (" As a > b false: " + d); System.out.println (" As b > c true: " + e); System.out.println (" As a < c false: " + f); } } On execution of the above example, you will get the output as – As a > b false: 45 As b > c true: 40 As a < c false: 15 6.2

Use of Switch ( with default & break)

The switch is a multiway branch statement. It can divert execution to different parts within the boundary of a program code section, observing the value of the switched expression. The general form of the switch statement will look like: switch (expression) { case value1: statement_seq1; break; case value2: statement_seq2; break; | | case valueN: statement_seqN; break; default: default_statements; } An example at this stage can make your conception more clear. // Example 6.5 Switch statement Demo public class SwitchDemo { public static void main() { for ( int month=1; month <= 12; month++)

switch (month) { case 1: System.out.println("There are 31 days in January."); break; case 2: System.out.println("There are 28 days in February if not Leap year."); System.out.println("February will have 29 days in Leap years."); break; case 3: System.out.println("There are 31 days in March."); break; case 4: System.out.println("There are 30 days in April."); break; case 5: System.out.println("There are 31 days in May."); break; case 6: System.out.println("There are 30 days in June."); break; case 7: System.out.println("There are 31 days in July."); break; case 8: System.out.println("There are 31 days in August."); break; case 9: System.out.println("There are 30 days in September."); break; case 10: System.out.println("There are 31 days in October."); break; case 11: System.out.println("There are 30 days in November."); break; default: System.out.println("There are 31 days in December."); } } } Run this program and see what comes out as the output (Picture 6.2). In this example we have made use of a loop statement for which is an iteration type control statement about which discussion will be made in the next chapter. The for statement changes the value of month, one by one, and the corresponding case statement gets executed. The break statement restarts the next looping operation. When the month value

reaches 12, no matching case is obtained within the program code boundary. Therefore, the default statement gets executed.

Picture 6.2 It may not be out of place to mention that the break statement has three uses: It terminates a statement sequence in a switch statement, as you have already seen. 2) It can be used to exit a loop. 3) It can sometimes act as a goto statement. 1)

Points 2 & 3 will be discussed in the next chapter. 6.3

Conclusions

Use of control statement becomes essential in a program when decisions about the different code paths is to be taken in a program by observing different internal conditions arising out of either change of input data or intermediate results obtained at intermediate steps before the final result is reached. Besides decision type control statements there are iteration or looping type control statements about which discussions will be made in the next chapter. Use of nested if and if-else statements in a program has been discussed in details. The importance of switch-case statements has also been explained with appropriate examples.

Related Documents


More Documents from "Prof. (Dr) Ananda M Ghosh."