Logical AND is defined as being true if and only if both of its predicates are true, otherwise it is false. So both A and B must be true for A AND B to be true. Look at the table below -

A
B
A && B
F F F
F T F
T F F
T T T

This is known as a truth table. It shows all of the possible states that A and B can be in. As you can see both A and B must be true for A && B to be true. Let us look at a concrete example. Remember the example on retaking exams?

We did it last time with a nested if. We could of done it with conjunction -

  1. if (mark < 50 && retake < 4){
  2. System.out.println("You must retake");
  3. } else{
  4. System.out.println("You do not need to retake");
  5. }

Next we will look at OR and NOT as well as how to do XOR :)

next more truth tables >>

 

 
Basics
Menu
Search