If Statements

If Statement

  • If statements can be used to excecute a method or a series of methods according to a condition if the condition is met. ## If Else Statement
  • Same thing as an if statements, but if something else is met other than the condition for the If Statement, then it would run a different line of code. ## If-Elseif-Else
  • If the condition of something is met, then run the code, if not then try to meet the next condition. If both are false then run the final condition.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);

double c1;
double c2;

System.out.print("Enter your first number: "); 

c1 = scanner.nextDouble();

System.out.println(x);

System.out.print("Enter the operator (+,-,*,/): "); // First input for operator choice

char operator = scanner.next().charAt(0);

System.out.println(operator);

System.out.print("Enter your second number: "); // Second input for operator chosen

c2 = scanner.nextDouble();

System.out.println(y);

if (operator == '+') {
  System.out.println("The answer is: "+ (c1 + c2)); // If addition is picked
} else if (operator == '-') {
  System.out.println("The answer is: "+ (c1 - c2)); // If subtraction is picked
} else if (operator == '*') {
  System.out.println("The answer is: "+ c1 * c2); // If multiplication is picked
} else if (operator == '/') {
  System.out.println("The answer is: "+ c1 / c2); // If division is picked
} else {
  System.out.println("Unknown Operation"); // If anything else is chosen
}
Enter your first number: 0.0
Enter the operator (+,-,*,/): /
Enter your second number: 0.0
The answer is: 1.6666666666666667
double c1; 
double c2;

// Take input from the user
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

// Take the inputs
c1 = scanner.nextDouble();

System.out.println(c1);

System.out.print("Enter the operator (+,-,*,/): ");

char operator = scanner.next().charAt(0);

System.out.println(operator);

System.out.print("Enter second number: ");

c2 = scanner.nextDouble();

System.out.println(c2);

double output = 0;

switch (operator) {

// Case to add 
case '+':

    output = c1 + c2;

    break;

// Case to subtract 
case '-':

    output = c1 - c2;

    break;

// Case to multiply 
case '*':

    output = c1 * c2;

    break;

// Case to divide 
case '/':

    output = c1 / c2;

    break;

default:

    System.out.println("Unknown input");

    break;
}

System.out.println("The final equation: ");

System.out.println(c1 + " " + operator + " " + c2 + " = " + output); // Final result
Enter the first number: 5.0
Enter the operator (+,-,*,/): /
Enter second number: 5.0
The final equation: 
5.0 / 5.0 = 1.0

De Morgan's Law

De Morgans Law of Union: The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B). This is also known as De Morgan's Law of Union. It can be represented as (A ∪ B)' = A' ∩ B'.

  • What this means is, we would think of it as, not A or B is not A and B. It should be oposite of the statement.