You are on page 1of 9

College of Information Engineering

Department of Computer Networks Engineering


OOPII(Java), 2nd Class

Lecture 3
Conditional Statements
3.1 If..else statement
The if single-selection statement performs an indicated action only when the
condition is true; otherwise, the action is skipped. The if…else double-selection
statement allows the programmer to specify an action to perform when the condition
is true and a different action when the condition is false.
If (condition)
Statment1;
else
Statment2;
For example, the pseudocode statement
If student’s grade is greater than or equal to 60
Print “Passed”
Else
Print “Failed”
prints “Passed” if the student’s grade is greater than or equal to 60, but prints
“Failed” if it is less than 60. In either case, after printing occurs, the next pseudocode
statement in sequence is “performed.” The preceding If…Else pseudocode statement
can be written in Java as:
if ( grade >= 60 )
System.out.println( "Passed" );
else
System.out.println( "Failed" );

1
Lecturer: Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

3.2 Conditional Operation (Operator Precedence)


Java provides the conditional operator (?:) that can be used in place of an
if…else statement. This is Java’s only ternary operator—this means that it takes three
operands. Together, the operands and the ?: symbol form a conditional expression.
The first operand (to the left of the ?) is a boolean expression (i.e., a condition
that evaluates to a Boolean value—true or false), the second operand (between the ?
and :) is the value of the conditional expression if the boolean expression is true and
the third operand (to the right of the :) is the value of the conditional expression if the
boolean expression evaluates to false.
System.out.println(condirion ? “value for true condition” : “value for false condition” );
For example, the statement

System.out.println( studentGrade >= 60 ? "Passed" : "Failed" );

prints the value of println’s conditional-expression argument. The conditional


expression in this statement evaluates to the string "Passed" if the boolean expression
student-Grade >= 60 is true and evaluates to the string "Failed" if the boolean
expression is false.
Thus, this statement with the conditional operator performs essentially the
same function as the if…else statement shown earlier in this section. The precedence
of the conditional operator is low, so the entire conditional expression is normally
placed in parentheses. Actually, the conditional expressions can be used in some
situations where if…else statements cannot.

3.3 Nested if…else Statements


A program can test multiple cases by placing if…else statements inside other
if…else statements to create nested if…else statements.

2
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

If (condition)
Statment1;
Else if (condition2)
Statment2;
Else Statment2;
For example, the following pseudocode represents a nested if…else that prints A for
exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for
grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other
grades:
If student’s grade is greater than or equal to 90
Print “A”
else
If student’s grade is greater than or equal to 80
Print “B”
else
If student’s grade is greater than or equal to 70
Print “C”
else
If student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
This pseudo code may be written in Java as

3
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

if ( studentGrade >= 90 )
System.out.println( "A" );
else
if ( studentGrade >= 80 )
System.out.println( "B" );
else
if ( studentGrade >= 70 )
System.out.println( "C" );
else
if ( studentGrade >= 60 )
System.out.println( "D" );
else
System.out.println( "F" );
If student Grade is greater than or equal to 90, the first four conditions will be true,
but only the statement in the if-part of the first if…else statement will execute. After
that statement executes, the else-part of the “outermost” if…else statement is skipped.
Most Java programmers prefer to write the preceding if…else statement as:
if ( studentGrade >= 90 )
System.out.println( "A" );
else if ( studentGrade >= 80 )
System.out.println( "B" );
else if ( studentGrade >= 70 )
System.out.println( "C" );
else if ( studentGrade >= 60 )
System.out.println( "D" );
else
System.out.println( "F" );
4
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

To force the nested if…else statement to execute as it was originally intended, it must
write it as follows:
if ( x > 5 )
{
if ( y > 5 )
System.out.println( "x and y are > 5" );
}
else
System.out.println( "x is <= 5" );

3.4 Relational Operators


The conditional statements such as (if, if…else, while, do…while and for
statements) each require a condition to determine how to continue a program’s flow
of control. So far, we have studied only simple conditions, such as: count <= 10,
number != sentinelValue and total > 1000.
Simple conditions are expressed in terms of the relational operators >, <, >=
and <= and the equality operators == and !=, and each expression tests only one
condition. To test multiple conditions in the process of making a decision, we
performed these tests in separate statements or in nested if or if…else statements.
Sometimes, control statements require more complex conditions to determine a
program’s flow of control.

3.5 Logical Operators

Java provides logical operators to enable you to form more complex conditions by
combining simple conditions. The logical operators are:

5
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

Logical Operator Description


&& (conditional AND)
|| (conditional OR)
& (boolean logical AND)
| (boolean logical inclusive OR)
^ (boolean logical exclusive OR)
! (logical NOT)

Example of Conditional AND (&&) Operator:


Suppose that we wish to ensure at some point in a program that two conditions are
both true before we choose a certain path of execution. In this case, we can use the
&& (conditional AND) operator, as follows:
if ( gender == FEMALE) && (age >= 65 )
++seniorFemales;

This if statement contains two simple conditions. The condition gender == FEMALE
compares variable gender to the constant FEMALE. This might be evaluated, for
example, to determine whether a person is female. The condition age >= 65 might be
evaluated to determine whether a person is a senior citizen. The if statement
considers the combined condition gender == FEMALE && age >= 65, which is true
if and only if both simple conditions are true. If the combined condition is true, the if
statement’s body increments seniorFemales by 1. If either or both of the simple
conditions are false, the program skips the increment. Some programmers find that
the preceding combined condition is more readable when redundant parentheses are
added, as in:
6
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

( gender == FEMALE ) && ( age >= 65 )

3.6 Bitwise Operator


The bitwise operators are bitwise AND (&), bitwise inclusive OR (|), bitwise
exclusive OR (^), left shift (<<), signed right shift (>>), unsigned right shift (>>>)
and bitwise complement (~). The bitwise AND, bitwise inclusive OR and bitwise
exclusive OR operators compare their two operands bit by bit. The bitwise AND
operator sets each bit in the result to 1 if and only if the corresponding bit in both
operands is 1. The bitwise inclusive OR operator sets each bit in the result to 1 if the
corresponding bit in either (or both) operand(s) is 1. The bitwise exclusive OR
operator sets each bit in the result to 1 if the corresponding bit in exactly one operand
is 1. The left-shift operator shifts the bits of its left operand to the left by the number
of bits specified in its right operand. The signed right shift operator shifts the bits in
its left operand to the right by the number of bits specified in its right operand—if the
left operand is negative, 1s are shifted in from the left; otherwise, 0s are shifted in
from the left. The unsigned right shift operator shifts the bits in its left operand to the
right by the number of bits specified in its right operand—0s are shifted in from the
left. The bitwise complement operator sets all 0 bits in its operand to 1 in the result
and sets all 1 bits in its operand to 0 in the result. The bitwise operators are
summarized below:

Bitwise Name Description


Operator
& bitwise AND The bits in the result are set to 1 if the corresponding bits in
the two operands are both 1.
| bitwise inclusive The bits in the result are set to 1 if at least one of the
OR corresponding bits in the two operands is 1.
^ bitwise exclusive The bits in the result are set to 1 if exactly one of the
OR corresponding bits in the two operands is 1.

7
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

<< left shift Shifts The bits of the first operand left by the number of bits
specified by the second operand; fill from the right with 0.
>> signed right shift Shifts the bits of the first operand right by the number of bits
specified by the second operand. If the first operand is
negative, 1’s are filled in from the left; otherwise, 0’s are
filled in from the left.
>>> unsigned right shift Shifts the bits of the first operand right by the number of bits
specified by the second operand; 0s are filled in from the left.
~ bitwise All 0 bits are set to 1, and all 1 bits are set to 0.
complement

3.6 Switch Condition


Java provides the switch multiple-selection statement to perform different actions
based on the possible values of an integer variable or expression. Each action is
associated with the value of a constant integral expression (i.e., a constant value of
type byte, short, int or char, but not long) that the variable or expression on which the
switch is based may assume. The general formula is:

Switch (condition)
{ case value1: statement;
break;
case value2: statement;
break;
.
.

Default: statement;;
} //end sitch

Example:
8
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry
College of Information Engineering
Department of Computer Networks Engineering
OOPII(Java), 2nd Class

// add 1 to appropriate counter for specified grade


public void incrementLetterGradeCounter( int Grade )
{
// determine which grade was entered
switch ( grade / 10 )
{
case 9: // grade was between 90
case 10: // and 100
++aCount; // increment aCount
break; // necessary to exit switch
case 8: // grade was between 80 and 89
++bCount; // increment bCount
break; // exit switch
case 7: // grade was between 70 and 79
++cCount; // increment cCount
break; // exit switch
case 6: // grade was between 60 and 69
default: // grade was less than 60
++fCount; // increment fCount
break; // optional; will exit switch anyway
++Count; // increment dCount
break; // exit switch
} // end switch
} // end method

9
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry

You might also like