Professional Documents
Culture Documents
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
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" );
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
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
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
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
9
Asst. Prof. Dr. Lahieb Mohammed Al-Yassiry