3/6: Logical Operators questions about previous program modifications logical operators program of...
-
Upload
leslie-hart -
Category
Documents
-
view
216 -
download
0
Transcript of 3/6: Logical Operators questions about previous program modifications logical operators program of...
3/6: Logical Operators
• questions about previous program modifications
• logical operators
• program of the day
Add in “how many circles”//Fig. 5.9: DoWhileTest.java
import java.awt.Graphics;import java.awt.Color; //brought in to change colorimport javax.swing.JApplet;import javax.swing.JOptionPane; //brought in for Input box
public class DoWhileTest extends JApplet { int repeat = 0; //used in both methods so declared outside
both public void init () { repeat = Integer.parseInt ( JOptionPane.showInputDialog ( "How many circles?" ) ); }
Add in color switching public void paint ( Graphics g ) { super.paint ( g ); int counter = 1; do { switch ( counter % 2 ) { case 0: g.setColor ( Color.red ); break; //put ‘break’s at case 1: //end of prev. line g.setColor ( Color.green ); break; //to save space } g.drawOval ( 110 - counter * 10 , 110 - counter * 10 , counter * 20 , counter * 20 ); ++counter; } while ( counter <= repeat ); }}
Logical Operators
• To evaluate more than one condition, we can use logical operators
• EX: if you have a ticket AND are at least 17, you can see an R-rated movie.
• EX: if it is after five o’clock OR I’m tired, I’ll leave work.
• EX: if the car is NOT out of gas, I’ll drive.
Logical operators
• && logical AND both must be true
• & boolean logical AND both must be true
• || logical OR one must be true
• | boolean logical inclusive OR one must be true
• ^ boolean logical exclusive OR only one can be true
• ! logical NOT reverses evaluation
Logical operators: && and &
• && AND both must be true
• & AND both must be true*
Truth table for && and & operators:
1st condition 2nd condition RESULT
false false false
false true false
true false false
true true true
Logical operators: && and &
• && AND both must be true
• & AND both must be true*
• The & operator differs in that both conditions are evaluated/processed every time. The && provides a “short-circuit” if the 1st condition isn’t true. Why it matters: EX: if you include an increment operator:
if ( gender == 1 & ++age == 1 );
Logical operators: || and |
• || OR at least one must be true
• | OR at least one must be true*
Truth table for || and | operators:
1st condition 2nd condition RESULT
false false false
false true true
true false true
true true true
Logical operators: || and |
• || OR at least one must be true
• | OR at least one must be true*
• The | operator differs in that both conditions are evaluated/processed every time. The || provides a “short-circuit” if the first condition IS true. Why it matters: EX: if you include an increment operator:
if ( gender == 1 | ++age == 1 );
Logical operators: ^
• ^ ONLY OR only one must be true
Truth table for the ^ operator:
1st condition 2nd condition RESULT
false false false
false true true
true false true
true true false
Logical operators: !
• ! NOT reverses evaluationUnary operator (not
binary)
Truth table for the ! operator:
condition RESULT
false true
true false
Logical operators: examples
( 3 > 4 && 8 > 5 ) results in
( false && true ) results in
( false )
• one way these operators can be used: as an alternative to nested if statements.
Program of the Day
• Create a program that will print out a table of 3 randomly generated double-type numbers from 0 to less than 100, then print out the largest of the 3 numbers in the MS-DOS window.
• Generate the three numbers by using the Math.random() method:
EX: x = Math.random() * 100 ;will generate a random double-type from 0 – 99.
• Use logical operators to decide which number is the largest.