If Statements

If statements are used to check whether a condition is true or not.

if (condition) {
    //output
}
else {
    //ouput
}

Else If Statements

Else if is used if there is another condition but it is not the direct opposite of the if statement.

if (condition) {
    //output
}
else if (other condition) {
    //ouput
}

Else Statements

Else is when the if condition is false then the else statement will come into play.

if (condition) {
    //output
}
else if (other condition) {
    //ouput
}
else {
    //output
}

If, else if, else grades

This is an if, else if and else program to check which grade the number inputted correlates to.

class IfandElse{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your score: ");
        int score = scanner.nextInt();

        if (score >= 90){
            System.out.println("This is an A grade");
        }

        else if (score >= 80) {
            System.out.println("This is a B grade");
        }

        else if (score >= 70) {
            System.out.println("This is a C grade");
        }

        else if (score >= 60) {
            System.out.println("This is a D grade");
        }

        else
            System.out.println("This is an F grade");
        }

}

IfandElse.main(null);
Enter your score: 
 50
This is an F grade

Switch Case with Grades

This is a switch case program to check which grade the number inputted correlates to.

import java.util.Scanner;  
public class Grade {
 
   public static void main(String[] args) {
      // score (0-100)
      String grade = null;
      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");
 
      switch(score/10) 
      {
        // for >= 90
        case 10:
        case 9:
           grade = "A";
           break;
        // for >= 80 & <90
        case 8:
           grade = "B";
           break;
        // for >= 70 & <80
        case 7:
           grade = "C";
           break;
        // for >= 60 & <70
        case 6:
           grade = "D";
           break;
        // for < 60
        default:
           grade = "F";
           break;
      }
      
      System.out.println("This is a " + grade + " grade");
   }
 
}
Grade.main(null)
Enter your score: 
Your score is: 64
This is a D grade

If and Else Monotonic Funtion

class Monotonic {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the length for the array: "); //user input for array
        int length = scanner.nextInt();
        int [] arr = new int [length];
        System.out.println("Enter the numbers for the array: "); // use these inputs to check if increasing or decreasing

            for (int i=0; i<length; i++) {
                arr[i] = scanner.nextInt();
            }
    System.out.println(Arrays.toString(arr));
        
    boolean answer = checkmonotonic(arr);
    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 checkmonotonic(int arr[])
  {
    int length = arr.length;
    boolean increasing = true;
    boolean decreasing = true;
    
    for (int j = 0; j < length - 1; j++) {
        if (arr[j] > arr[j+1]) { // if the number after the first number is smaller then the numbers in the array are not increasing
            increasing = false;
        }
        
        else if (arr[j] < arr[j+1]) { // else if the number after the first number is larger then the numbers in the array are not decreasing
            decreasing = false;
        }
        
        else { 
            increasing = true;
            decreasing = true;
        }
    }
    
        
        return increasing || decreasing; // if the array is increasing or decreasing then according to the above if outputs then the array is monotonic otherwise if both are true then not monotonic
        
    }
    
    
    
}



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

De Morgan's Law with Monotonic Function

De Morgan's Law is used to negate an expression that we already have. So for !(a && b) it would be come !a || !b as all the elements flip even the operators. For !(a < b) it would become a >= b. For !(a == b) it would become a != b.

class MonotonicDeMorgans {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the length for the array: "); //user input for array
        int length = scanner.nextInt();
        int [] arr = new int [length];
        System.out.println("Enter the numbers for the array: "); // use these inputs to check if increasing or decreasing

            for (int i=0; i<length; i++) {
                arr[i] = scanner.nextInt();
            }
    System.out.println(Arrays.toString(arr));
        
    boolean answer = checkmonotonic(arr);
    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 checkmonotonic(int arr[])
  {
    int length = arr.length;
    boolean increasing = true;
    boolean decreasing = true;
    
    for (int j = 0; j < length - 1; j++) {
        if (!(arr[j] <= arr[j+1])) { // if the number after the first number is smaller then the numbers in the array are not increasing
            increasing = false;
        }
        
        else if (!(arr[j] >= arr[j+1])) { // else if the number after the first number is larger then the numbers in the array are not decreasing
            decreasing = false;
        }
        
        else { 
            increasing = true;
            decreasing = true;
        }
    }
    
        
        return increasing || decreasing; // if the array is increasing or decreasing then according to the above if outputs then the array is monotonic otherwise if both are true then not monotonic
        
    }
    
    
    
}

MonotonicDeMorgans.main(null);
Enter the length for the array: 
 6
Enter the numbers for the array: 
 9
 8
 7
 6
 5
 4
[9, 8, 7, 6, 5, 4]
Yes the function is monotonic