Boolean Expressions and If Statements
if, if else statements, switch-case, and De Morgan's law
- If Statements
- If-else Statements
- Else-if Statements
- 5 Condition If
- Switch Code
- De Morgan's Law
- Resources Used
boolean alcaraz=true;
if (alcaraz=true){ //if Alcaraz wins the US Open mens final
System.out.print("woohoo! youngest champion since 1990!"); //Alcaraz will become the US open champion since
}
boolean alcaraz=true;
if (alcaraz=true){ //if Alcaraz wins the US Open mens final
System.out.print("woohoo! alcaraz is youngest champion since 1990!"); //Alcaraz will become the youngest US open champion since Sampras
}
else { //otherwise (if Alcaraz loses)
System.out.print("Ruud is the first US open champion from Norway!!"); //Ruud will become the first major champion winner from Norway
}
int temp=15;
if (temp<=10) { //if the temperature is less than or equal to 10 degrees following will output
System.out.print("snuggle up with hot tea and watch the Sound of Music");
}
else if (temp>10 && temp<=25) {//if the temperature if greater than 10 degreed but less than or equal to 25 degrees following will output
System.out.print("a hike in the wild wild wilderness");
}
else { //any other temperature (greater than 25 degrees) will output
System.out.print("beach day!");
}
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
String userInputStr;
double time;
Scanner inputStream = new Scanner(System.in);
System.out.print("What time is it? \n");
userInputStr = inputStream.nextLine();
time = Double.parseDouble(userInputStr);
if (time>=6 && time<=10) {
System.out.print("Good Morning Sunshine!");
}
else if (time>10 && time<=13) {
System.out.print("Lunch Time!");
}
else if (time>13 && time<=17) {
System.out.print("Good Afternoon!");
}
else if (time>17 && time<=20) {
System.out.print("Dinner Time!");
}
else {
System.out.print("Good Night, Sweet Dreams!");
}
}
}
Greetings.main(null);
import java.util.Scanner;
public class Day {
public void go() {
Scanner scan = new Scanner(System.in);
int day = scan.nextInt();
switch (day) {
case 1: // case 1, if matches print following
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default: // other cases print out following
System.out.println("That is not a day of the week");
}
}
public static void main(String[] args) {
Day cond = new Day();
cond.go();
}
}
Day.main(null);
if (!((1 == 1) && (1 == 2))) { // if either 1 does not equal 1 or 1 does not equal 2, will print
System.out.println("1 does not equal 2");
}
else {
System.out.println ("a miracle has occurred");
}
if (!((1 == 1) || (1 == 2))) { // if 1 is equal to 1 or equal to 2 will print
System.out.println("a miracle has occurred");
}
else {
System.out.println ("1 does equal 1");
}