Docsity

Java: Boolean Expressions, Operators & Comparisons, Slides of Java Programming

Partial preview of the text

Download Java: Boolean Expressions, Operators & Comparisons and more Slides Java Programming in PDF only on Docsity!

Boolean expressions

Highlights

  • Complex expressions
  • Short-circuit evaluation

Complex expressions

Two boolean operators: && is the AND operations || is the OR operations

Complex expressions

If statements for when x... ... is between 10 and 20 (inclusive) Cannot say: 10 <= x <= 20 (why?) ... is a vowel (x is type char)

Double trouble!

When comparing doubles, you should use check to see if relative error is small: Math.abs((x-y)/x) < 10E- (double has about 16 digits of accuracy so you could go to 10E-15 if you want) For comparing Strings, use: string1.equals(string2);

Short-circuit evaluation

Short-circuit evaluation is when you have a complex boolean expression (&& or ||) but you don't need to compute all parts. If this is true, then if will immediately run (See: ShortCircuit.java)

Break time

Lexicographic and Alphabetic

(Not that important, don't bother waking up) Every character has an associated integer (See: Lexicon.java) To get alphabetic ordering, you need to ignore case

Precedence

Book gives a big precedence table of everything (even stuff we haven't learned yet) on page 126. Java uses this table (and any parenthesis you put in) to “fully parenthesize” lines. Example: x = 3+ becomes: (x = (3+2))

Precedence

Example: x = 3+ becomes: (x = (3+2)) Java evaluation algorithm:

  1. Go from left to right until you hit a )
  2. Evaluate this expression in ( ) and replace it with the value
  3. Repeat from 1 until done.

Precedence

Write simple/clear expressions and you don't need to worry about this! If someone else wrote it and you have to fix it (Kitty code), slug through this process to find what is going on