Implementing Boolean into if else statements

boolean isTired = true; // setting boolean to true.
if(isTired) { // isTired is the same thing as if(isTired == True)
   System.out.println("Go to sleep");
} else { 
    System.out.println("KeepWorking");
}
Go to sleep

if-elseif-elseif-elsif-else statement, 5 or more conditions

int day= 4;

if(day==1){
    System.out.println("Sunday");
}
else if(day==2){
    System.out.println("Monday");

}
else if(day==3){
    System.out.println("Tuesday");
}
else if(day==4){
    System.out.println("Wednesday");
}
else if(day==5){
    System.out.println("Thursday");
}
else if(day==6){
    System.out.println("Friday");
}
else if(day==7){
    System.out.println("Saturday");
}
Wednesday
int day=3;

switch(day){
    
    case 1:
        System.out.println("Sunday");
         break;
    case 2:
        System.out.println("Monday");
        break;
     case 3:
        System.out.println("Tuesday");
        break;
    case 4:
        System.out.println("Wednesday");
         break;
    case 5:
        System.out.println("Thursday");
         break;
    case 6:
         System.out.println("Friday");
          break;
     case 7:
          System.out.println("Saturday");
           break;
          
}
Tuesday
int x = 4, y = 3;
if (!(x < 3 || y > 2))
{
   System.out.println("True");
}
else
{
   System.out.println("False");
}

Now let's switch up! Time to make things easier and use switch case

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

int x = 4, y = 3;
if (!(x < 3 && y > 2))
{
   System.out.println("True);
}
else
{
   System.out.println("False");
}

DeMorgan’s laws were developed by Augustus De Morgan in the 1800s. They show how to handle the negation of a complex conditional, which is a conditional statement with more than one condition joined by an and (&&) or or (||), such as (x < 3) && (y > 2)

Basic logic gates There are seven basic logic gates: AND, OR, XOR, NOT, NAND, NOR, and XNOR.

AND | OR | XOR | NOT | NAND | NOR | XNOR

The AND gate is so named because, if 0 is called "false" and 1 is called "true," the gate acts in the same way as the logical "and" operator. The following illustration and table show the circuit symbol and logic combinations for an AND gate. (In the symbol, the input terminals are at left and the output terminal is at right.) The output is "true" when both inputs are "true." Otherwise, the output is "false." In other words, the output is 1 only when both inputs one AND two are 1.

Homework

2009 frq 3b 2017 frq 1b 2019 frq 2b

College Board 2009 frq

College Board 2017 frq

College Board 2019 frq

//2009 3b



private int getChargingCost(int startHour, int chargeTime)
{
  int cost = 0;
  int rIndex = startHour;
  int hoursRemaining = chargeTime;  


  
  while(hoursRemaining > 0)
  {
    cost += rateTable[rIndex];

    rIndex++;
    if(rIndex == rateTable.length)
      rIndex = 0;
      
    hoursRemaining--;
  }
  
  return cost;

  //final cost returned
}
//2017 1b

public boolean isStrictlyIncreasing()
{
    for (int i = 0; i < digitList.size()-1; i++)  //checks if values increasing
{
    if (digitList.get(i).intValue() >= digitList.get(i+1).intValue())
{
    return false;
}
}
    return true;
}
//2019 frq 2b


public static int dayOfWeek(int month, int day, int year)
{
  int weekday = firstDayOfYear(year);
  int additionalDays = dayOfYear(month, day, year) - 1;

  for(int d = 1; d <= additionalDays; d++)
  {
    weekday++; //add one to weekday

    if(weekday == 7)
      weekday = 0; //set to 0
  }
        
  return weekday; 
}

The Truth Table

The following table (also called a truth table) shows the result for P && Q when P and Q are both expressions that can be true or false. An expression involving logical operators like (P && Q) evaluates to a Boolean value, true or false. As you can see below the result of P && Q is only true if both P and Q are true.

Compound Boolean Expression

What if you want to go out and your parents say you can go out if you clean your room and do your homework? Run the code below and try different values for cleanedRoom and didHomework and see what they have to be for it to print You can go out.

public class Main
{
   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");
     }
   }
}

Main.main(null)
No, you can't go out