If and Expressions
Blog post about different coding elements.
- If Statement
- If/Else Statement
- If/Else Example
- If-elseIf-else Statement
- If/Else example with 5 or more Conditions
- Switch-case Example
- De Morgan's Law
if(condition) {
examplecode();
}
if(condition) {
runprimarycodehere();
}else{
runsecondarycodehere();
}
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a number: ");
// Get input
number = scanner.nextInt();
System.out.println(number);
// Check if number is less than 0
if(number < 0) {
System.out.println(number + " is negative");
}else{
System.out.println(number + " is positive");
}
If-elseIf-else Statement
-
Runs an if statement for a condition. If the condition is met then a set of code is run. If the condition is not met then a separate if statement is run. If that condition is met then a set of code is ran but if not then the else code is run.
-
In this example, we made the positive negative calculator more specific. It now tests if the number is 0 first. If it is then it states that it's not negative or positive. While if it's not then it tests if the number is greater than 0 in which case it's positive. If not then it's negative.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a number: ");
number = scanner.nextInt();
System.out.println(number);
//Check if number equals 0
if(number == 0) {
System.out.println(number + " is not negative or positive");
//If it doesn't then check if number is greater than 0.
}else if (number > 0) {
System.out.println(number + " is positive");
}
else {
System.out.println(number + " is negative");
}
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a number: ");
try {
// Get input
number = scanner.nextInt();
System.out.println(number);
// Check if number is equal to zero
if(number == 0) {
System.out.println(number + " is 0");
// If it's not then check if number is greater than 0
}else if (number > 0) {
if(number > 100) {
System.out.println(number + " is positive and greater than 100");
}
else {
System.out.println(number + " is positive and less than 100");
}
}
// If number is less than 0...
else if (number < 0) {
if(number > -100) {
System.out.println(number + " is negative and greater than -100");
}
else {
System.out.println(number + " is negative and less than -100");
}
}
} catch (Exception e) {
System.out.println("Input must be an integer");
}
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int number;
String message;
//Setup of COLOR variables
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String PURPLE = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String WHITE = "\u001B[37m";
// Message Input
System.out.print("Enter a message: ");
message = scanner.nextLine();
System.out.print(message + "\n\n");
// Menu for custom color.
System.out.print(RED + "0 - Red\n");
System.out.print(GREEN + "1 - Green\n");
System.out.print(YELLOW + "2 - Yellow\n");
System.out.print(BLUE + "3 - Blue\n");
System.out.print(PURPLE + "4 - Purple\n");
System.out.print(CYAN + "5 - Cyan\n");
System.out.print(WHITE + "6 - White\n\n");
System.out.print("Enter a number: ");
number = scanner.nextInt();
System.out.print(number + "\n\n");
// Switch-case related to the chosen number.
switch(number) {
case 0:
System.out.println(RED + message);
break;
case 1:
System.out.println(GREEN + message);
break;
case 2:
System.out.println(YELLOW + message);
break;
case 3:
System.out.println(BLUE + message);
break;
case 4:
System.out.println(PURPLE + message);
break;
case 5:
System.out.println(CYAN + message);
break;
case 6:
System.out.println(WHITE + message);
break;
}
int x = 10;
int y = 4;
if(!(x < 3) || !(y > 2) == !(x < 3 && y > 2)) {
System.out.print("True");
}else {
System.out.print("False");
}