Branching Control Statements
Branching Control Statements
The if statements
The if-else statement
switch
The conditional operators
The if Statement Like most languages, JAVA uses the keyword if to implement
the decision control instruction. The general form of if statement looks like
this: if (this condition is true) execute this statement; The keyword if tells the
compiler that what follows is a decision control instruction. The condition
following the keyword if is always enclosed within a pair of parentheses. If the
condition, whatever it is, is true, then the statement is executed. If the
condition is not true then the statement is not executed; instead the program
skips past it. But how do we express the condition itself in JAVA? And how do
we evaluate its truth or falsity? As a general rule, we express a condition using
JAVA’s ‘relational’ operators. The relational operators allow us to compare two
values to see whether they are equal to each other, unequal, or whether one is
greater than the other. Here’s how they look and how they are evaluated in
JAVA.
(b) if ( condition )
{
do this ;
and this ;
}
(c) if ( condition )
do this ;
else
do this ;
(d) if ( condition )
{
do this ;
and this ;
}
else
{
do this ;
and this ;
}
(e) if ( condition )
do this ;
else
{
if ( condition )
do this ;
else
{
do this ;
and this ; }}
(f) if ( condition )
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
else
do this ;
per=(m1+m2+m3+m4)*100/500;
if(per>=60)
System.out.println(“First division”);
else
{
if(per>=50)
System.out.println(“Second division”);
else
{
if(per>=40)
System.out.println(“Third division”);
else
System.out.println(“Fail”);
}
}
}
}
This is a straight forward program. Observe that the program uses
nested if-elses. This leads to three disadvantages:
(a) As the number of conditions go on increasing the level of indentation also
goes on increasing. As a result the whole program creeps to the right.
(b) Care needs to be exercised to match the corresponding ifs and elses.
(c)Care needs to be exercised to match the corresponding pair of braces.
All these three problems can be eliminated by usage of ‘Logical operators’. The
following program illustrates this.
public class Main
{
public static void main(String args[])
{
int m1,m2,m3,m4,m5,per;
Scanner sc= new Scanner(System.in);
System.out.println(“Enter marks five subject marks”);
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
m4=sc.nextInt();
m5=sc.nextInt();
per=(m1+m2+m3+m4)*100/500;
if(per>=60)
System.out.println(“First division”);
if(per>=50&&per<60)
System.out.println(“Second division”);
if(per>=40&&per<50)
System.out.println(“Third division”);
if(per<40)
System.out.println(“Fail”);
}
}
As can be seen from the second if statement, the && operator is
used to combine two conditions. ‘Second division’ gets printed if
both the conditions evaluate to true. If one of the conditions
evaluate to false then the whole thing is treated as false.
Two distinct advantages can be cited in favour of this program:
(a) The matching (or do I say mismatching) of the ifs with their
corresponding elses gets avoided, since there are no elses in this program.
(b) In spite of using several conditions, the program doesn't creep to the right.
In the previous program the statements went on creeping to the right. This
effect becomes more pronounced as the number of conditions go on
increasing. This would make the task of matching the ifs with their
corresponding elses
And matching of opening and closing braces that much more difficult.
The else if Clause
There is one more way in which we can write program for Example. This
involves usage of else if blocks as shown below:
public class Main
{
public static void main(String args[])
{
int m1,m2,m3,m4,m5,per;
Scanner sc= new Scanner(System.in);
System.out.println(“Enter marks five subject marks”);
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
m4=sc.nextInt();
m5=sc.nextInt();
per=(m1+m2+m3+m4)*100/500;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}
}
You can note that this program reduces the indentation of the
statements. In this case every else is associated with its previous if.
The last else goes to work only if all the conditions fail. Even in
else if ladder the last else is optional.
Note that the else if clause is nothing different. It is just a way of
rearranging the else with the if that follows it. This would be
evident if you look at the following code:
if ( i == 2 )
printf ( "With you…" ) ;
else {
if ( j == 2 )
printf ( "…All the time" ) ;
}
if(i==2)
printf ( "With you…" ) ;
else if ( j == 2 )
printf ( "…All the time" ) ;
The ! Operator
So far we have used only the logical operators && and ||. The third logical
operator is the NOT operator, written as !. This operator reverses the result of
the expression it operates on. For example, if the expression evaluates to a
non-zero value, then applying ! operator to it results into a 0. Vice versa, if the
expression evaluates to zero then on applying ! operator to it makes it 1, a
non-zero value. The final result (after applying !) 0 or 1 is considered to be false
or true respectively. Here is an example of the NOT operator applied to a
relational expression.
! ( y < 10 )
This means “not y less than 10”. In other words, if y is less than 10, the
expression will be false, since ( y < 10 ) is true. We can express the same
condition as ( y >= 10 ). The NOT operator is often used to reverse the logical
value of a single variable, as in the expression
if ( ! flag )
This is another way of saying
if ( flag == 0 )
Does the NOT operator sound confusing? Avoid it if you want, as the same
thing can be achieved without using the NOT operator
Hierarchy of Operators Revisited
Since we have now added the logical operators to the list of operators we
know, it is time to review these operators and their priorities. summarizes the
operators we have seen so far. The higher the position of an operator is in the
tabe , higher is its priority.
(B) char a ;
int y ;
a=sc.next().charAt(0);
y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
Here 1 would be assigned to y if a >=65 && a <=90 evaluates to
true, otherwise 0 would be assigned.