If Statements

If statements can be used to evaluate whether a condition is true or not.

if (grade >= 70){ // condition is only true if a person's grade in the class is over 70%
    System.out.print("You have passed the class");
}

Else If Statements

Else if statements are used to check if another condition, other than the one that was checked first, is true or not.

if (grade >= 70){ // condition is only true if a person's grade in the class is over 70%
    System.out.print("You have passed the class");
}
else if (grade >= 60){
    System.out.print("You can pass the class by completing extra credit work");
}

Else Statements

Else statements allow for an alternative statement to be executed if a previous condition had evaluated to false.

if (grade >= 70){ // condition is only true if a person's grade in the class is over 70%
    System.out.print("You have passed the class");
}
else{ // if the person does not earned a passing grade of 70% or above, they have not passed the class
    System.out.print("You did not pass the class");
}

If, Else If, Else Grades

In this case, If statements, Else If statements, and Else statements are used to check what letter grade a number corresponds with.

if (grade >= 90){ // condition is only true if a person's grade in the class is over 90%
    System.out.print("You have earned an A in the class");
}
else if (grade >=80){ // if a person earned a 80% or above but below 90% they earned a B in the class
    System.out.print("You have earned a B in the class");
}
else if (grade >=70){ // if a person earned a 70% or above but below 80% they earned a C in the class
    System.out.print("You have earned a C in the class");
}
else{ // if a person earned below a 70% in the class, they did not pass the class
    System.out.print("You did not pass the class");
}

Switch Case With Grades

A switch case is an if-else ladder than can be used to check multiple conditions at once. In this case the program is checking what letter grade a particular numerical grade corresponds with.

import java.util.Scanner; 

public class GradeSwitch{
    public static void main(String[] args){
        String grade;
        int gradepoint;
        System.out.print("Enter your score: \n");  
        Scanner scanner = new Scanner(System.in); 
        int score = scanner.nextInt(); 
        System.out.print("Your score is: ");  
        System.out.print(score + "\n");
        int scoremultipleoften = score/10; // int rounds down

        // the grade brackets are 10 points each, so we can divide by 10 to find each letter grade
        switch(scoremultipleoften){
            case 10:
                grade = "A+";
                gradepoint = 4;
                break;
            case 9:
                grade = "A";
                gradepoint = 4;
                break;
            case 8:
                grade = "B";
                gradepoint = 3;
                break;
            case 7:
                grade = "C";
                gradepoint = 2;
                break;
            case 6:
                grade = "D";
                gradepoint = 1;
                break;
            default:
                grade = "F";
                gradepoint = 0;
                break;
        }
        System.out.println("The letter grade you earned is a " + grade);
        System.out.println("The grade point you earned is a " + gradepoint);
    }
}
GradeSwitch.main(null)
Enter your score: 
Your score is: 95
The letter grade you earned is a A
The grade point you earned is a 4

If and Else Monotonic Function

If statements and else statements are used to evaluate whether an array of numbers is monotonic.

import java.util.Scanner; 

class Monotonic {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the length for the array: "); // user decides the total length the array
        int length = scanner.nextInt();
        int [] array = new int [length];
        System.out.println("Enter the numbers for the array: "); // user decides the specific numbers to put in the array
            for (int i=0; i<length; i++) {
                array[i] = scanner.nextInt();
            }
    System.out.println(Arrays.toString(array));
        
    boolean answer = monotonicchecker(array);
    if (answer == true) // if the array is only increasing or decreasing then it is monotonic
    System.out.print("Yes, the function is monotonic");
    else // if the array is doing both increasing and decreasing then it is not monotonic
    System.out.print("No, the function is not monotonic");

    }
}

public static boolean monotonicchecker(int array[]){
    boolean ascending=false;
    boolean descending=false;
    for (int i=0; i<array.length-1; i++){
        if (array[i]>array[i+1]){
            descending=true;
        }
        else if (array[i]<array[i+1]){
            ascending=true;
        }
        else{
            if(descending && !ascending){
                ascending=true;
            }
            else if(ascending && !descending){
                descending=true;
            }
        }
        if(ascending && descending){
            return false;
        }
    }
    return true;
}

Monotonic.main(null);
Enter the length for the array: 
Enter the numbers for the array: 
[1, 2, 3]
Yes, the function is monotonic

De Morgan's Law

De Morgan's Law allows for boolean expressions to be written in different ways, but still produce the same result. The || means "or" and the && means "and."

import java.util.Scanner; 

class Monotonic {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the length for the array: "); // user decides the total length the array
        int length = scanner.nextInt();
        int [] array = new int [length];
        System.out.println("Enter the numbers for the array: "); // user decides the specific numbers to put in the array
            for (int i=0; i<length; i++) {
                array[i] = scanner.nextInt();
            }
    System.out.println(Arrays.toString(array));
        
    boolean answer = monotonicchecker(array);
    if (answer == true) // if the array is only increasing or decreasing then it is monotonic
    System.out.print("Yes, the function is monotonic");
    else // if the array is doing both increasing and decreasing then it is not monotonic
    System.out.print("No, the function is not monotonic");

    }
}

public static boolean monotonicchecker(int array[]){
    boolean ascending=false;
    boolean descending=false;
    for (int i=0; i<array.length-1; i++){
        if (!(array[i]<=array[i+1])){ // changed logic to reflect De Morgan's Law
            descending=true;
        }
        else if (!(array[i]>=array[i+1])){ // changed logic to reflect De Morgan's Law
            ascending=true;
        }
        else{
            if(descending && !ascending){
                ascending=true;
            }
            else if(ascending && !descending){
                descending=true;
            }
        }
        if(!(!ascending || !descending)){ // changed logic to reflect De Morgan's Law
            return false;
        }
    }
    return true;
}

Monotonic.main(null);import java.util.Scanner;
Enter the length for the array: 
Enter the numbers for the array: 
[1, 2, 3]
Yes, the function is monotonic