Boolean Expressions and If Statements
The following code blocks demonstrate if, if-else, and if-elseif-else. The print statments explain why each expression is shown or why it is not printed. It is known that when overall boolean is true, the statment is printed. Similarly, if the boolean is false, the statment is not printed.
// Boolean Expressions
// if
boolean value = true;
if(value == true){
System.out.println("the statment is printed if the value is set to true");
}
if(value){
System.out.println("the statment is printed since the value is already set to true");
}
// Boolean Expressions continued
// if-else
boolean value = false;
if(value == true){
System.out.println("the statment is printed if the value is set to true");
} else {
System.out.println("else this statment is printed since the value is not true");
}
if(value != false) {
System.out.println("if the value is not equal to false, then this statment will print");
} else {
System.out.println("else this statment is printed since the value is equal to false");
}
// if-else if-else
boolean value1 = true;
boolean value2 = true;
if(value1 && !value2){
System.out.println("false!");
// statment is not printed because the overall expression is false
}
else if(!value1 && value2){
System.out.println("false!");
// statment is not printed because the overall expression is false
}
else if(!value1 && !value2){
System.out.println("false!");
// statment is not printed because the overall expression is false
}
else if(!value1 || !value2){
System.out.println("false!");
// statment is not printed because the overall expression is false
}
else{
System.out.println("all the expressions are false! this is our last resort");
// statment is not printed because the overall expression is false
}
if (true) {
System.out.println("the statment is printed becuase the condition set to true");
}
if (true && !false) {
System.out.println("the statment is printed becuase the condition is true and not false which makes it true");
}
if (true || false) {
System.out.println("the statment is printed becuase the condition is true or false, which results in true");
}
if ((true && !false) && (true || false)) {
System.out.println("the statment is printed becuase the condition is: (true and not false) and (true or false) which results in true");
}
if (!((false || !true) || (false && true))) {
System.out.println("the statment is printed becuase the condition is: (not (false or false) or (false and true)) which results in true");
}
// De Morgan's laws
boolean a = true;
boolean b = false;
if (!((b || a) && (!b && !a))) // NOT(A || B) is Equivalent to (NOT(A) AND NOT(B))
{
System.out.println("demorgan first law");
}
if (!((a && b) && (!a || !b))) // NOT(A && B) is Equivalent to (NOT(A) OR NOT(B))
{
System.out.println("demorgan second law");
}
if else
if else statement is a conditional statement
the if line is executed if the specified statment is determined to be true.
if the condition is false, the if statment is not executed and the next statment is moved on to. else if statment is executed when the first if statment is not executed. else if is a another condition
else is the final and false statment
switch gives the option to select a block of code
import java.util.Scanner;
public class ifelse {
int number;
int remainder() {
int rem = (number%2);
System.out.println("Remainder: " + rem);
if( rem == 0) {
System.out.println("Even");
// if rem is equal to 0, then "even" will be printed
} else {
System.out.println("Odd");
// if rem is not equal to 0, then "odd" will be printed
}
return rem;
}
public ifelse(int n) {
System.out.println("Number entered: " + n);
number = n;
}
}
explanation
the input is an integer. the given integer is divided by 2 to determine if the remainder is 0 or 1. the remainder is tested to see if it is equal to 0. if the remainder is equal to 0, then the number is determined to be even. else the number is odd, because the remainder is not 0 and there is only one other possibility therefore another else if is not required.
public class gradeifelse {
private double grade;
public double finalgrade() {
if ( grade >= 90) {
System.out.println("A");
// if the grade is greater than equal to 90, then A will be printed
} else if( grade >= 80 && grade < 90) {
System.out.println("B");
// if the previous statment is not true and the grade is
// greater than equal to 80 or less than 90, then B will be printed
} else if( grade >= 70 && grade < 80) {
System.out.println("C");
// if the previous statment is not true and the grade is
// greater than equal to 70 or less than 80, then C will be printed
} else if( grade >= 60 && grade < 70) {
System.out.println("D");
// if the previous statment is not true and
//the grade is greater than equal to 60 or less than 70, then D will be printed
} else if( grade >= 0 && grade < 60) {
System.out.println("F");
// if the previous statment is not true and the grade is less than 60, then F will be printed
} else {
System.out.println("Not within range");
// if the previous statments are not true and an
// integer is not inputted properly, then the else statment will be printed
}
return grade;
}
public gradeifelse(double n) {
System.out.println("Grade entered: " + n);
grade = n;
}
}
import java.util.Scanner;
public class gradeswitch {
private int value = 0;
private double grade;
public gradeswitch(double n) {
System.out.println("Grade entered: " + n);
grade = n;
}
public void switchgrade() {
if (grade >= 90) {
value = 1;
} else if( grade >= 80 && grade < 90) {
value = 2;
} else if( grade >= 70 && grade < 80) {
value = 3;
} else if( grade >= 60 && grade < 70) {
value = 4;
} else if( grade >= 0 && grade < 60)
value = 5;
switch(value) { //switch case with values
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
case 5:
System.out.println("F");
break;
default:
System.out.println("not number assigned correctly");
}
}
}
public class Menu {
// Instance Variables
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
public final String[][] COLORS = { // 2D Array of ANSI Terminal Colors
{"Default",DEFAULT},
{"Red", "\u001B[31m"},
{"Green", "\u001B[32m"},
{"Yellow", "\u001B[33m"},
{"Blue", "\u001B[34m"},
{"Purple", "\u001B[35m"},
{"Cyan", "\u001B[36m"},
{"White", "\u001B[37m"},
};
// 2D column location for data
public final int NAME = 0;
public final int ANSI = 1; // ANSI is the "standard" for terminal codes
Scanner sc; // available to all inside the menu class
// Constructor on this Object takes control of menu events and actions
public Menu() {
sc = new Scanner(System.in); // using Java Scanner Object
this.print(); // print Menu
boolean quit = false;
while (!quit) {
try { // scan for Input
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.println("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
// Print the menu options to Terminal
private void print() {
//System.out.println commands below is used to present a Menu to the user.
System.out.println("-------------------------\n");
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Even or Odd");
System.out.println("2 - Grade - with if else");
System.out.println("3 - Grade - with switch case");
System.out.println("0 - Quit");
System.out.println("-------------------------\n");
}
// Private method to perform action and return true if action is to quit/exit
private boolean action(int selection) {
boolean quit = false;
double g;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
System.out.println("Enter number: ");
int n = sc.nextInt();
ifelse mynumber = new ifelse(n);
mynumber.remainder();
break;
case 2:
System.out.println("Enter grade: ");
g = sc.nextDouble();
gradeifelse mygrade = new gradeifelse(g);
mygrade.finalgrade();
break;
case 3:
System.out.println("Enter grade: ");
g = sc.nextDouble();
gradeswitch grade = new gradeswitch(g);
grade.switchgrade();
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
// Static driver/tester method
static public void main(String[] args) {
new Menu(); // starting Menu object
}
}
Menu.main(null);