- < - less than
- > - greater than
- <= - less than or equal to
- >= - greater than or equal to
- != - not equal to
- == - equal to

int n1=5; int n2=7; System.out.println(n1>n2); //false; System.out.println(n1<n2); //true; System.out.println(6>=8); //false; System.out.println(6<=8); //true; System.out.println(n1!=5); //false; System.out.println(n1==5); //true;

These operators DO NOT work on the String type or any reference type variables. This is because of how primitives and reference types are stored.

The equality operator (==) checks and compares the value that is stored in the variable space. This works for primitives as they store their value, but reference variables store another location. So, even if the reference variables have the same information, they are pointing to different locations.

Instead, we have to use methods provided by the reference variable classes. Most programmers/designers will decide what it means for two of their objects to be equal (.equals) OR for one to be 'greater' or 'less' than another (.compareTo). Typically, both the equals and compareTo methods are provided in a class and take another variable of the same type as a parameter.

String name="Sally"; System.out.println(name.equals("Earl")); //false System.out.println(name.equals("Sally")); //true

- 0 if they are equal
- positive if r1 is greater than r2
- negative if r1 is less than r2

In the String class, the compareTo method orders the Strings alphabetically.

String input="apple"; System.out.println(input.compareTo("apple")); //0 System.out.println(input.compareTo("aaron")); //15 System.out.println(input.compareTo("banana")); //-1

In the above examples, note that when compared to itself, the result is 0. When compared to something smaller( alphabetically before), the result is positive (because the calling object is bigger) and when compared to something larger (alphabetically after), the result is negative (because the calling object is smaller). The actual number is irrelevant - we only need to know if it is positive, negative or zero.

- the world is round
- grass is green

The world is round AND the grass is green.Since both statements are true, the entire statement is TRUE.

The world is round AND the grass is blue The world is flat AND the grass is green. The world is flat and the grass is red.In line(1), the first part of the statement is true, but the second part is false. So, the whole statement evaluates to FALSE.

In line(2), the first part is false and the second part is true. The whole statement evaluates to FALSE.

In line(3), both parts are false so the statement is FALSE.

An AND statement evaluates to true if and only if BOTH parts of the statement is true.

age==18 && zip==19446

Notice how age==18 will evaluate to true or false based on the value of age, as will zip==19446.

The world is round OR the grass is green. The world is round OR the grass is purple. The world is a cube OR the grass is green. The world is a pyramid OR the grass is orange.In line(1), both parts are true so the statement evaluates to TRUE.

In line(2), the first part is true and the second part is false. But, because it is an or, the entire statements is TRUE.

In line(3), the first part is false and the second part is true. Again, the statement evaluates to TRUE.

It is only in the fourth example, line(4), that we find the statement to be false, because both parts are false.

An OR statement evaluates to true if either part is true.

When writing code, the same is true in a disjunction as in a conjunction, both parts MUST be able to evaluate to true or false on their own. Let's look at an example where we want to know if a user's input is a 4 or a 5:

input==4 || input==5

A common mistake is that students write input==4 || 5. This is NOT correct as the right side (5) DOES NOT evaluate to true or false as it is not a standalone condition.

!(5==5)In the above example, the value of the statement is false because 5==5 is true and the negation of that is false.

- !(a&&b) => !a || !b
- !(a||b) => !a && !b

Using DeMorgan's Law, the following two statements are equivalent:

!(input==4 || input==5)

input!=4 && input!=5

To begin, truth tables are a key element of a branch of mathematics called logic. Often, we use symbolic logic to shorten the amount of writing that we need to do. Symbolic logic works by substituting a variable for a boolean expression. For example, if we are given the following condition:

age>=16 && testScore>=80

Using symbolic logic, we can declare the following variables:

- p = age>=16
- q = testScore>=80

The variables p and q are chosen randomly. You can use any letters that you choose.

Using symbolic logic we can now represent the same statement using

p && qEach of these statements can have a value of true or false. So, together, we have four different possibilities:

p | q |
---|---|

T | T |

T | F |

F | T |

F | F |

To avoid missing any combinations, we can set up our T/F values in each column using the following process:

- In the first column, take the number of rows/2. Write that many T rows followed by the same number of F rows.
- In the next column, take the number of T from the first column and cut it in half, alternating sets of that number of T followed by that number of F, until all the rows are filled.
- This process of halving repeats until the final column is reached, at which point the values alternate T and F in each row.

a | b | c |
---|---|---|

T | T | T |

T | T | F |

T | F | T |

T | F | F |

F | T | T |

F | T | F |

F | F | T |

F | F | F |

Explanation:

We begin by taking each of the individual boolean relations and assigning them a variable:

a = grade==12

b = prereq>=90

c = grade>=10

Then, with three variables, we know that there is 2^{3}=8 rows.

The first column will have 8/2=4 consecutive rows of T followed by 4 consecutive rows of F.

The second columns will have 4/2=2 rows of T follwed by 2 rows of F. That pattern will repeat until out of rows.

The final column will alternate T/F on each row.

We begin by taking each of the individual boolean relations and assigning them a variable:

a = grade==12

b = prereq>=90

c = grade>=10

Then, with three variables, we know that there is 2

The first column will have 8/2=4 consecutive rows of T followed by 4 consecutive rows of F.

The second columns will have 4/2=2 rows of T follwed by 2 rows of F. That pattern will repeat until out of rows.

The final column will alternate T/F on each row.

Often, in more complicated truth tables, we can build the process in steps.

a | b | c | b&&c | a||(b&&c) |
---|---|---|---|---|

T | T | T | T | T |

T | T | F | F | T |

T | F | T | F | T |

T | F | F | F | T |

F | T | T | T | T |

F | T | F | F | F |

F | F | T | F | F |

F | F | F | F | F |

Explanation:

Step 1: Using only the values from the b and c columns, we generate the value of (b&&c) for each row. In the blue highlighted example, you will see that T && T => T.

Step 2: Once the fourth column is complete, we use the a column and the (b&&c) column to generate the last column, which is the value of the truth table. In the red highlighted example, a=F and (b&&c)=T. When combined with an OR statement, the final result is T.

What we now know is the truth value of the statement for each possible set of boolean relational values. For example, if all of the original conditions are false, the statement will be false. However, if the first value is true (grade==12), it doesn't matter what the other values are, the statement is true.

Step 1: Using only the values from the b and c columns, we generate the value of (b&&c) for each row. In the blue highlighted example, you will see that T && T => T.

Step 2: Once the fourth column is complete, we use the a column and the (b&&c) column to generate the last column, which is the value of the truth table. In the red highlighted example, a=F and (b&&c)=T. When combined with an OR statement, the final result is T.

What we now know is the truth value of the statement for each possible set of boolean relational values. For example, if all of the original conditions are false, the statement will be false. However, if the first value is true (grade==12), it doesn't matter what the other values are, the statement is true.

a | b | c | a||(b&&c) |
---|---|---|---|

F | T | T | T |

In this example, a is false, b is true and c is true. This means that when we run the program, we need data that meets the following conditions:

- grade!=12 (a is false)
- prereq>=90 (b is true)
- grade>=10 (c is true)