Java Chap6 Decision Making Statements (Prof. Ananda M Ghosh.)
Java Chap6 Decision Making Statements (Prof. Ananda M Ghosh.)
Java Chap6 Decision Making Statements (Prof. Ananda M Ghosh.)
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.
Java has two main selection statements – if and switch. The general form of if-
statement 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.
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 condition
Next Statement
in Sequence
Fig-6.1 shows the flow control pattern of if-then and if-then-else constructs.
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.
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.
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.
As a > b false: 45
As b > c true: 40
As a < c false: 15
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.
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:
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.