3/6: Logical Operators questions about previous program modifications logical operators program of...

13
3/6: Logical Operators • questions about previous program modifications • logical operators • program of the day

Transcript of 3/6: Logical Operators questions about previous program modifications logical operators program of...

Page 1: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

3/6: Logical Operators

• questions about previous program modifications

• logical operators

• program of the day

Page 2: 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?" ) ); }

Page 3: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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 ); }}

Page 4: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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.

Page 5: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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

Page 6: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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

Page 7: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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 );

Page 8: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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

Page 9: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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 );

Page 10: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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

Page 11: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

Logical operators: !

• ! NOT reverses evaluationUnary operator (not

binary)

Truth table for the ! operator:

condition RESULT

false true

true false

Page 12: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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.

Page 13: 3/6: Logical Operators questions about previous program modifications logical operators program of the day.

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.