If and Else Lesson
My lesson for If and Else
- If Statements
- Else If Statements
- Else Statements
- If, else if, else grades
- Switch Case with Grades
- If and Else Monotonic Funtion
- De Morgan's Law with Monotonic Function
If statements are used to check whether a condition is true or not.
if (condition) {
//output
}
else {
//ouput
}
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 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
}
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);
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)
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);
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);