Boolean Expressions

  • Boolean expressions are used to illustrate true or false statements in code. They use a data type called boolean to format the code.
import java.util.Scanner;
public class Booleans //define class
{
    public static void main(String[] args) //java main method
{
    boolean isSunny = true; //declare the boolean variables
    boolean isRainy = false; 
    if (isSunny)
    {
        System.out.println("It is sunny.");
    }
    else
    {
        System.out.println("It is rainy.");
    }
}
}
Booleans.main(null);
It is sunny.

If else Statements

  • If else statements are used in Java to execute a certain action if certain conditions are met. If the conditions are not met, there may be an "else" statement to perform a different action.
  • If statements can be simple or complex in nature. Here is an example of a simple if statement
import java.util.Scanner;
public class Test //define class
{
    public static void main(String[] args) //java main method
{
    int num = 10; //declare the integer variable 

    if (num > 2) //if statement format with parameters 
    {
        System.out.println("The number is greater than 2"); //if the number is greater than zero, print this statement. 
    }
}
}
Test.main(null);
The number is greater than 2
  • If statements often use else statements to specify what the code should do if the first condition is not met.
import java.util.Scanner;
public class Else 
{
    public static void main(String[] args)
{
    int num = 1; 
    if (num > 2)
    {
        System.out.println("The number is greater than 2");
    }
    else //the computer will run this portion if the number is less than or equal to 2
    {
        System.out.println("The number is not greater than 2"); //This is what the computer will print
    }
}
}
Else.main(null);
The number is not greater than 2

Switch Statements

  • Switch statements can be used in adition to if-else statements to switch between different commands in code. For exmple, a user can enter whitch case they would like to run or that computer will go through all of the switches.
import java.util.Scanner;
public class switchExample
{
    public static void main(String[] args)
{
    int num = 1; 
    switch(num)
    {
    case 1: 
        if (num > 2)
        {
            System.out.println("The number is greater than 2");
        }
        else //the computer will run this portion if the number is less than or equal to 2
        {
            System.out.println("The number is not greater than 2"); //This is what the computer will print
        }
    case 2: 
        if (num > 3)
        {
            System.out.println("The number is greater than 3");
        }
        else //the computer will run this portion if the number is less than or equal to 2
        {
            System.out.println("The number is not greater than 3"); //This is what the computer will print
        }
    case 3: 
        if (num > 7)
        {
            System.out.println("The number is greater than 7");
        }
        else //the computer will run this portion if the number is less than or equal to 2
        {
            System.out.println("The number is not greater than 7"); //This is what the computer will print
        }
    case 4: 
        if (num < 3)
        {
            System.out.println("The number is less than 3");
        }
        else //the computer will run this portion if the number is less than or equal to 2
        {
            System.out.println("The number is greater than 3"); //This is what the computer will print
        }
    case 5: 
        if (num > 4)
        {
            System.out.println("The number is not greater than 4");
        }
        else //the computer will run this portion if the number is less than or equal to 2
        {
            System.out.println("The number is greater than 4"); //This is what the computer will print
        }
    case 6:
    System.out.println("Thanks for running this code.");
}
}
}
switchExample.main(null);
The number is not greater than 2
The number is not greater than 3
The number is not greater than 7
The number is less than 3
The number is greater than 4
Thanks for running this code.

De Morgan's Law

  • De Morgan's Law is a way to negate "and" or "or" statements in code. Below is a demonstration on how the law works
import java.util.Scanner;
public class deMorgan //define class
{
    public static void main(String[] args) //java main method
{
    int a = 0; //initializing variables
    int b = 0;

    System.out.println(!(a==10 || b==10)); //Will print true if a or b does not equal 10
    System.out.println(a!=10 && b!=10); 
}
}
deMorgan.main(null);
true
true
  • De Morgan's law can be used in other code. For example between different foods.
import java.util.Scanner;
public class deMorganExample //define class
{
    public static void main(String[] args) //java main method
{
    String food = "pasta";
    String food2 = "pizza";

    if (!(food.equals("pizza") || food.equals("bread"))) // Makes !(false value == true value)
    {
        System.out.println("The food is not pasta or pizza."); //Will print this
    }
    else // if not
    {
        System.out.print("Food type is pasta or pizza."); //Will print this
    }
}
}
deMorganExample.main(null);
Food is not pasta or pizza.