Unit 3 If Then Statements
public class Example1
{
public static void main(String[] args)
{
boolean cleanedRoom = true;
boolean didHomework = false;
if (cleanedRoom && didHomework)
{
System.out.println("You can go out");
}
else
{
System.out.println("No, you can't go out");
}
}
}
A Truth Table shows all the possible results of a boolean function, whether it's true or false.
De Morgan's Law:
De Morgan's Law states that the "!" symbol will negate an And statement and an Or statement. This means that if a condition is written as a || b, !(a || b) would really be a && b. This goes for the other way around where !(a && b) is a || b. It will also cause a true statement to be false with !(true) and vice versa.
boolean child = true;
boolean hungry = false;
if (!(child && hungry)) {
System.out.println("You have a hungry child");
}
else {
System.out.println("Who knows what's going on?");
}
boolean fruit = true;
boolean red = true;
if (!(!(fruit) || !(red))) {
System.out.println("It's probably a red apple");
}
else {
System.out.println("You have an unknown food");
}