Java Conditional Statements Post
Java conditional statements post for AP CSA
Java if else statements are a type of conditional statement. If the Java if statement evaluates to true, it will perform a specific action designated to it. Otherwise, if it evaluates to false, it will perform another designated action.
import java.util.Scanner;
import java.lang.Math;
public class ifelse {
public static void main(String[] args) {
System.out.println("Pick a school subject:");
System.out.println("----------------------");
Scanner userInput = new Scanner(System.in);
String schoolSubject = userInput.nextLine();
String schoolSubjectNew = schoolSubject.toLowerCase();
if (schoolSubjectNew.equals("math")) {
System.out.println("Math Fact: The only factors of a prime number is 1 and itself.");
} else if (schoolSubjectNew.equals("english")) {
System.out.println("English Fact: An independent clause is a statement of words that contains at least 1 subject and 1 verb.");
} else if (schoolSubjectNew.equals("history")) {
System.out.println("History Fact: The 1st president of the United States of America was George Washington.");
} else if (schoolSubjectNew.equals("physical education")) {
System.out.println("Physical Education Fact: Soccer is the best sport in the world!");
} else if (schoolSubjectNew.equals("science")) {
System.out.println("Science Fact: The derivative of velocity is acceleration");
} else if (schoolSubjectNew.equals("computer science")) {
System.out.println("Computer Science Fact: An if else statement performs an action if the condition is met, and performs another action if the condition is not met.");
} else {
System.out.println("The school subject you typed was not found...");
System.out.println("Miscellaneous Fact: Zach Lavine is one of the best NBA basketball players");
}
}
}
ifelse.main(null);
import java.util.Scanner;
import java.lang.Math;
public class Switch {
public static void main(String[] args) {
System.out.println("Pick a school subject:");
System.out.println("----------------------");
Scanner userInput = new Scanner(System.in);
String schoolSubject = userInput.nextLine();
String schoolSubjectNew = schoolSubject.toLowerCase();
switch(schoolSubjectNew) {
case "math":
System.out.println("Math Fact: The only factors of a prime number is 1 and itself.");
break;
case "english":
System.out.println("English Fact: An independent clause is a statement of words that contains at least 1 subject and 1 verb.");
break;
case "history":
System.out.println("History Fact: The 1st president of the United States of America was George Washington.");
break;
case "physical education":
System.out.println("Physical Education Fact: Soccer is the best sport in the world!");
break;
case "science":
System.out.println("Science Fact: The derivative of velocity is acceleration");
break;
case "computer science":
System.out.println("Computer Science Fact: An if else statement performs an action if the condition is met, and performs another action if the condition is not met.");
break;
default:
System.out.println("The school subject you typed was not found...");
System.out.println("Miscellaneous Fact: Zach Lavine is one of the best NBA basketball players");
break;
}
}
}
Switch.main(null);
The De Morgan's Law basically uses the logical operators or, and, nor, and nand. For two conditions paired with an ||(or) operator, the program will evaluate to true if at least one of the conditions evaluates to true. For two conditions paired with an &&(and) operator, the program will only evaluate to true only if both of the conditions evaluate to true. For two conditions paired with a !(not) operator, the program is reversed, as it will print true if the condition evaluates to false, and print false if the condition evaluates to true.
import java.util.Scanner;
import java.lang.Math;
public class DeMorgan1 {
public static void main(String[] args) {
System.out.println("Type the length in minutes of your average work out:");
System.out.println("-----------------------------------------");
Scanner userInput = new Scanner(System.in);
int minutesNew = userInput.nextInt();
if (minutesNew > 0 && minutesNew <= 60) {
System.out.println("Your average work outs should be a little longer unless it is really intense");
} else if (minutesNew > 60 && minutesNew <= 120) {
System.out.println("Your work outs are at perfect length");
} else if (minutesNew > 120 && minutesNew <= 210) {
System.out.println("Your work outs are at an above average length");
} else {
System.out.println("You are a beast");
}
}
}
DeMorgan1.main(null);
import java.util.Scanner;
import java.lang.Math;
public class DeMorgan2 {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter your first integer:");
System.out.println("-------------------------");
int numberOne = userInput.nextInt();
System.out.println("Enter your second integer:");
System.out.println("-------------------------");
int numberTwo = userInput.nextInt();
boolean booleanValue1 = numberOne == numberTwo;
System.out.println("Enter your third integer:");
System.out.println("-------------------------");
int numberThree = userInput.nextInt();
System.out.println("Enter your fourth integer:");
System.out.println("-------------------------");
int numberFour = userInput.nextInt();
boolean booleanValue2 = numberThree == numberFour;
if (booleanValue1 || booleanValue2) {
System.out.println("Or Statement: True");
} else {
System.out.println("Or Statement: False");
}
if (booleanValue1 && booleanValue2){
System.out.println("And Statement: True");
} else {
System.out.println("And Statement: False");
}
if (!(booleanValue1 || booleanValue2)){
System.out.println("Nor Statement: True");
} else {
System.out.println("Nor Statement: False");
}
if (!(booleanValue1 && booleanValue2)){
System.out.println("Nand Statement: True");
} else {
System.out.println("Nand Statement: False");
}
}
}
DeMorgan2.main(null);