Title: CSCI1402: Lecture 2 Week 6
1CSCI1402 Lecture 2 Week 6
Dr. David A. Elizondo Centre for Computational
Intelligence School of Computing Office Gateway
6.61 email elizondo_at_dmu.ac.uk
2Short Circuit Logical Operators
- Normal (AND) and (OR) operands will always
evaluate each operand. - Short circuit (AND) (OR) will evaluate the
second operand only when necessary (more
efficient). - In an AND operation, if the 1st operand is false,
the outcome is false, no matter what value the
2nd operand. - In an OR operation, if the 1st operand is true,
the outcome of the operation is true no matter
what the value of the second operand.
3Short Circuit Logical Operators
Short circuit operator prevents division by 0
- public class SCops
- public static void main (String args)
- int n, d, q
- n 10
- d 2
- if (d ! 0 (n d) 0)
- System.out.println(d is a factor of
n) - d 0 // now, set d to zero
- // Since d is zero, the 2nd operand is not
evaluated - if (d ! 0 (n d) 0)
- System.out.println(d is a factor of n)
- // Now, try same thing without short circuit
operator - // This will cause a divide by zero error.
- if (d ! 0 (n d) 0)
- System.out.println(d is a factor of n)
-
Now both expressions are evaluated allowing a
division by 0
4Selection switch statements
- It provides a multi way branch.
- It enables a program to select among several
alternatives. - Switch is generally more efficient than nested
ifs - The value of an expression is successfully tested
against a list of constants. When a match is
found, the statement sequence associated with
that match is executed.
5Selection switch statements
- General form of a switch statement
- switch(expression)
- case constant1
- statement sequence
- break
- case constant2
- statement sequence
- break
- case constant3
- statement sequence
- break
- .
- .
- .
- default
- statement sequence
6Selection switch statements
- The switch expression must be of type char, byte,
short, or int. (floating-point expressions are
not allowed). - Often the expression controlling the switch is
simply a variable. - The case constants must be literals of a type
compatible with the expression. - No two case constants in the same switch can have
identical values.
7Selection switch statements
- The default statement sequence is executed if no
case constant matches the expression. - The default is optional if not present, no
action takes place if all matches fail. - When a match is found, the statements associated
with that case are executed until the break is
encountered or, in the case of default or the
last case, until the end of the switch is reached.
8Selection switch statements
- Sample program
- public class SwitchDemo
- public static void main(String args )
- int i
- for (i0ilt10i)
- switch(i)
- case 0
- System.out.println(i is zero)
- break
- case 1
- System.out.println(i is one)
- break
- case 2
- System.out.println(i is two)
- break
- case 3
- System.out.println(i is three)
- break
- case 4
9Selection switch statements
- Program output
- i is zero
- i is 0ne
- i is two
- i is three
- i is four
- i is five or more
- i is five or more
- i is five or more
- i is five or more
- i is five or more
10The break statement
- It is technically optional (most applications
of the switch will use it). - When encountered within a statement sequence of a
case, the break statement causes program flow to
exit from the entire switch. - If a break statement does not end the statement
sequence associated with a case, then all
statements at and following the matching case
will be executed until a break (or the end of the
switch) is encountered.
11No break sample program
- class Nobreak
- public static void main(String args)
- int i
- for(i0ilt5i)
- switch(i)
- case 0
- System.out.println(i is less than one)
- case 1
- System.out.println(i is less than two)
- case 2
- System.out.println(i is less than three)
- case 3
- System.out.println(i is less than four)
- case 4
- System.out.println(i is less than five)
-
- System.out.println()
-
-
The case statements fall through here
12No break sample program
- Program output
- i is less than one
- i is less than two
- i is less than three
- i is less than four
- i is less than five
- i is less than two
- i is less than three
- i is less than four
- i is less than five
13No break sample program
- Program output
- i is less than three
- i is less than four
- i is less than five
- i is less than four
- i is less than five
- i is less than five
- Execution continues into the next case if no
break statement is present
14Empty cases
- You can have empty cases
- switch(i)
- case 1
- case 2
- case 3 System.out.println(i is 1, 2, or 3)
- break
- case 4 System.out.println(i is 4)
- break
-
- If i has the value 1,2, or 3, the 1st println()
statement executes. If it is 4, the 2nd println()
statement executes.
15Nested switch statements
- It is possible to have a switch as part of the
sequence of an outer switch. - This is called a nested switch.
- Even if the case constants of the inner and outer
switch contain common values, no conflicts will
arise.
16Nested switch statements
- Program sample
- switch(ch1)
- case A System.out.println(This is part of
an outer switch) - switch(ch2)
- case A System.out.println(This is part of
an inner switch) - break
- case B //...
- // end of inner case
- break
- case B // ...