IF Statement

An If statement is a conditional statement that, when true, runs the code inside the statement.

//condition inside if statment is true so system outprints "Hi"
if (true) {
    System.out.println("Hi");
}

//true and not false so system prints out "Hello"
if (true && !false) {
    System.out.println("Hello");
}
Hi
Hello

If/Else Statement

The If/Else statement extends the If statement by specifying an action if the If (true/false expression) is false. With the If statement, a program will execute the true code block or do nothing. With the If/Else statement, the program will execute either the true code block or the false code block so something is always executed with an If/Else statement.

int x = 10;

//if statement is false so the else statment runs
if (x<5) {
    System.out.println("Hello");
}
else {
    System.out.println("Greater than 5");
}
Greater than 5

If/ElseIf Statement

The If/ElseIf statement allows you to create a chain of If statements. You are able to test more than two conditions unlike the If/Else statment. The If statements are evaluated in order until one of the If expressions is true or the end of the If/ElseIf chain is reached. If the end of the If/ElseIf chain is reached without a true expression, no code blocks are executed.

int x = 9;

//false conditions until last else if statement so that one runs
if (x<2){
    System.out.println("Less than 2");
}
else if (x<4){
    System.out.println("Less than 4");
}
else if (x<6){
    System.out.println("Less than 6");
}
else if (x<8){
    System.out.println("Less than 8");
}
else if (x<10){
    System.out.println("Less than 10");
}
Less than 10

If/ElseIf/Else Statement

If/ElseIf/Else statements are the same as If/ElseIf statements except they have a trailing Else statement added at the end. This way there is executable code if none of the If or Else If statments are true.

char x = 'd';

//none of the if or elseif statments are true so it goes through the chain and runs the else statment
if (x=='a'){
    System.out.println("x is a");
}
else if (x=='b'){
    System.out.println("x is b");
}
else if (x=='c'){
    System.out.println("x is c");
}
else {
    System.out.println("not a, b, or c");
}
not a, b, or c

If/ElseIf/Else statments can be used to decide different program outputs depending on what the user inputted. For example, they would be helpful in a menu program.

Scanner inputStream = new Scanner(System.in);
String userInputStr;
int choice;

System.out.println("choose an integer 1-5: ");
userInputStr = inputStream.nextLine();
choice = Integer.parseInt(userInputStr);  //get user input for choice
System.out.println(choice);

//false conditions until choice equals 4 and only that else if statement runs
if (choice==1){
    System.out.println("\nyou chose one");
}
else if (choice==2){
    System.out.println("\nyou chose two");
}
else if (choice==3){
    System.out.println("\nyou chose three");
}
else if (choice==4){
    System.out.println("\nyou chose four");
}
else if (choice==5){
    System.out.println("\nyou chose five");
}
else {
    System.out.println("invalid input");
}
choose an integer 1-5: 
4

you chose four

Switch Statement

A switch statement is usually more efficient than a set of nested ifs. The switch statement is a control flow statement. It evaluates a user selection and different parts of code can be executed quickly. Below, a switch statement will preform the same function as the chain of nested ifs above.

Scanner inputStream = new Scanner(System.in);
String userInputStr;
int choice;

System.out.println("choose an integer 1-5: ");
userInputStr = inputStream.nextLine();
choice = Integer.parseInt(userInputStr);
System.out.println(choice);

switch (choice) {  //based on what the user inputted for the choice variable, different cases will run
    case 1:
        System.out.println("\nyou chose one");
        break;
    case 2:
        System.out.println("\nyou chose two");
        break;
    case 3:
        System.out.println("\nyou chose three");
        break; //default prevents it from moving on to and running the following cases
    case 4:
        System.out.println("\nyou chose four");
        break;
    case 5:
        System.out.println("\nyou chose five");
        break;
    default:  
        System.out.println("invalid input");  
}
choose an integer 1-5: 
4

you chose four

Boolean Expressions

A Boolean expression is a logical statement that is either True or False. Boolean expressions can compare any type of data if both parts of the expression have the same basic data type. A Boolean expression can be a combination of the Boolean constants true or false.

Logic Gates

Logic gates are used to carry out logical operations on single or multiple binary inputs and give one binary output. In simple terms, logic gates are the electronic circuits in a digital system.

This is an image

  • Buffer/YES : output is the same as the input
  • NOT : output is the opposite of the input
  • AND : returns true if both inputs are true
  • NAND : returns false if both inputs are true
  • OR : returns true if at least one of the inputs are true, returns false if both inputs are false
  • NOR : returns false if at least one of the inputs are true, returns true if both inputs are false
  • XOR : returns true if the two inputs are different
  • XNOR : returns true if two inputs are the same

This is an image

These tables demonstrate the input and output combinations of different logic gates

  • Tips to help remember the symbols:
    • each main shape is unique to a logic gate (ex: triangle = buffer/YES)
    • a small circle represents the addition of "NOT" to the logic gate
    • a small curved line represents the addition of "exclusive" or "X" to the logic gate

De Morgan's Law

DeMorgan's laws tell us how to transform logical expressions (with multiple AND and/or OR statements) using the NOT operator.

  • (not(A and B)) is equal to (not A or not B)
  • (not(A or B)) is equal to (not A and not B)
  • (not(A and B)) is not equal to (not A and not B)

This is an image

Syntax

  • union symbol (∪) : OR
  • intersection symbol (∩) : AND
  • apostrophe (') : NOT
boolean a = true;
boolean b = false;

//Not (A and B)
boolean result1 = !(a && b);

//Not A or Not B
boolean result2 = ((!a) || (!b));

if (result1 == result2){
    System.out.println("result 1 and 2 are the same");  
}
else{   //result 1 and 2 will always be equal, the code should never reach here
    System.out.println("Error"); 
}

//Not (A or B)
boolean result3 = !(a || b);

//Not A and Not B
boolean result4 = ((!a) && (!b));

if (result3 == result4){
    System.out.println("result 3 and 4 are the same");  
}
else{   //result 3 and 4 will always be equal, the code should never reach here
    System.out.println("Error"); 
}
result 1 and 2 are the same
result 3 and 4 are the same

Truth Tables

A truth table is a breakdown of a logic function by listing all possible values the function can attain. Such a table typically contains several rows and columns, with the top row representing the logical variables and combinations, in increasing complexity leading up to the final function. This is an image