Computer Programing Alnahrain Unversity Lecture-9
Computer Programing Alnahrain Unversity Lecture-9
Programming Fundamentals in
Java
Lecture Nine
II
Subjects:
9.1
9.2
9.3
9.4
switch Statement
break Statement
continue Statement
Logical Operators
9.4.1
9.4.2
9.4.3
9.4.4
9.4.5
9.4.6
2011
Example: write a program to read a student grades for one course (each mark is
between 0-100) then count the number of A, B, C, D, and F grades, the summation
and average of grades and display them on screen.
Grade
A
B
C
D
F
Mark range
90-100
80-89
70-79
60-69
0-59
// Program 1
import java.util.Scanner;
public class Switch_Ex
{
public static void main(String[] args)
{
int mark;
int markCo = 0;
int sum = 0;
double avg = 0.0;
int aCo = 0, bCo = 0, cCo = 0, dCo = 0, fCo = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter integer grades in the range
0-100");
System.out.println("Type any negative number to stop");
System.out.print("Enter a mark: ");
mark = in.nextInt();
while(mark >= 0)
{
switch(mark / 10)
{
case 9:
case 10:
Ihab A. Mohammed
2011
Ihab A. Mohammed
2011
In the above program, when co is equal to 5, the if condition becomes true and the
break statement executes which terminates and exit the for loop to execute the println
statement.
2011
9.4.1
Suppose that we want to add 5 marks to a student only and only if his mark is
between 45 and 49, which means that we have two conditions that both must be true:
(mark >=45)
and (mark <= 49)
We can write it without using logical operator As follows:
if(mark >= 45)
if(mark < 50)
mark += 5;
and by using conditional AND operator, the above code can be written as follows:
if(mark >= 45 && mark < 50)
mark += 5;
The conditional AND operator gives true only and only if all conditions are true.
Note: Many programmers prefer to use parentheses for each condition to be more
readable, thus the above code can be written as follows:
if( (mark >= 45) && (mark < 50) )
mark += 5;
9.4.2
expression2
False
True
False
True
Suppose that we want to select a car and we want its color to be either white or gray,
which means that the color is either red or blue (but not both which is impossible) so
we can write it without using logical operators as follows:
if(color == "white")
// Select the car
else if(color == "gray")
// Select the car
now by using conditional OR operator, the above code can be written as follows:
if( (color == "white") || (color == "gray") )
// Select the car
2011
expression1
False
False
True
True
9.4.3
expression2
False
True
False
True
expression1 || expression2
False
True
True
True
The parts of an expression containing && or || operators are evaluated only until
it is known whether the condition is true or false. Thus, evaluation of the expression:
(mark >= 45) && (mark < 50)
stops immediately if mark is less than 45 (because the first condition is false, so there
is no need to continue because the entire expression is false) and continues if mark is
greater than or equal to 45 (the first condition is true, so the entire expression could
still be true if the second condition is true also) this is called short-circuit
evaluation.
The evaluation of the expression:
(color == "white") || (color == "gray")
continues to the second condition because even if the first condition is false, the
second may be true which makes the entire expression true.
To understand the need for these operators, take a look at the following expression:
( i != 0 ) && ( 10 / i == 2 )
If i == 0 then evaluation stops immediately because the entire expression is false, but
if the evaluation continues, an error will occur because 10 / 0 is an error. So it is very
useful to use conditional logical operators to handle this kind of expressions without
any errors.
9.4.4
The boolean logical AND (&) and boolean logical inclusive OR (|) operators
work identically to the && (conditional AND) and || (conditional OR) operators, with
one exception: The boolean logical operators always evaluate both of their operands
(i.e., they do not perform short-circuit evaluation). Therefore, the expression:
( carSpeed > 100 ) & ( maxSpeed == 100 )
Ihab A. Mohammed
2011
Evaluates (maxSpeed == 100) regardless of whether carSpeed is > 100 (the second
condition is evaluated even if carSpeed is <= 100). This is useful if the right operand
of the boolean logical AND or boolean logical inclusive OR operator has a required
side effect modification of a variable's value. For example, the expression
( birthday == true ) | ( ++age >= 65 )
guarantees that the condition ++age >= 65 will be evaluated. Thus, the variable age is
incremented in the preceding expression, regardless of whether the overall expression
is true or false.
Note: For clarity, avoid expressions with side effects in conditions. The side effects
may look clever, but they can make it harder to understand code and can lead to
subtle logic errors.
9.4.5
9.4.6
expression2
False
True
False
True
expression1 ^ expression2
False
True
True
False
The ! (logical NOT, also called logical negation or logical complement) operator
enables a programmer to "reverse" the meaning of a condition. Unlike the logical
operators &&, ||, &, | and ^, which are binary operators that combine two conditions,
the logical negation operator is a unary operator that has only a single condition as an
operand. The logical negation operator is placed before a condition to choose a path
of execution if the original condition (without the logical negation operator) is false,
as in the program segment:
if( !(grade < 50))
System.out.println("Passed");
Note: in the above example, the parentheses are needed because it had a high priority
than the less than operator.
Ihab A. Mohammed
2011
Ihab A. Mohammed
2011
Ihab A. Mohammed
2011