3.5 Compund Boolean expressions
Nested conditional statements
definition: if statements within other if statements
public class Main {
public static void main(String[] args) {
int age = 20;
boolean isStudent = true;
// Outer if-else block
if (age >= 18) {
// Nested if-else block inside the first condition
if (isStudent) {
System.out.println("You are an adult and a student.");
} else {
System.out.println("You are an adult.");
}
} else {
System.out.println("You are not an adult.");
}
}
}
Main.main(null);
You are an adult and a student.
Simple Example of a Nested Conditional Statement
Let’s look at a very basic example of a nested conditional statement using if-else
blocks.
Scenario:
We want to check if a person is an adult and, if they are, we want to know if they are also a student.
Compound Conditional Statement
A compound conditional statement is when two or more conditions are combined into a single if
statement using logical operators like &&
(AND), ||
(OR), or !
(NOT). This allows us to check multiple conditions at once without needing to nest if
statements.
Logical Operators:
&&
(AND): True if both conditions are true.||
(OR): True if at least one condition is true.!
(NOT): Reverses the result of a condition (true becomes false, and false becomes true).
Example of a Compound Conditional Statement
Let’s say we want to check if a person is an adult and a student at the same time. Instead of using a nested if
statement, we can use a compound conditional statement.
public class Main {
public static void main(String[] args) {
int age = 20;
boolean isStudent = true;
// Compound conditional using && (AND)
if (age >= 18 && isStudent) {
System.out.println("You are an adult and a student.");
} else {
System.out.println("Either you are not an adult, or you are not a student.");
}
}
}
Main.main(null);
You are an adult and a student.
common mistake: Dangling else
- Java does not care about indentation
- else always belongs to the CLOSEST if
- curly braces can be use to format else so it belongs to the FIRST 'if'
Popcorn hack
- explain the purpose of this algorithm, and what each if condition is used for
- what would be output if input is
- age 20
- anual income 1500
- student status: yes
// function checkMembershipEligibility() {
// // Get user input
// let age = parseInt(prompt("Enter your age:")); // Age input
// let income = parseFloat(prompt("Enter your annual income:")); // Annual income input
// let isStudent = prompt("Are you a student? (yes/no):").toLowerCase() === 'yes'; // Student status input
// // Initialize an empty array to hold results
// let results = [];
// // Check eligibility for different memberships
// // Basic Membership
// if (age >= 18 && income >= 20000) {
// results.push("You qualify for Basic Membership.");
// }
// // Premium Membership
// if (age >= 25 && income >= 50000) {
// results.push("You qualify for Premium Membership.");
// }
// // Student Discount
// if (isStudent) {
// results.push("You are eligible for a Student Discount.");
// }
// // Senior Discount
// if (age >= 65) {
// results.push("You qualify for a Senior Discount.");
// }
// // If no eligibility, provide a default message
// if (results.length === 0) {
// results.push("You do not qualify for any memberships or discounts.");
// }
// // Output all results
// results.forEach(result => console.log(result));
// }
// // Call the function to execute
// checkMembershipEligibility();
Purpose of the Algorithm
The checkMembershipEligibility function determines if a user qualifies for different memberships or discounts based on their age, annual income, and student status.
Explanation of Each If Condition
Basic Membership: Checks if the user is 18+ years old and has an income of $20,000+. If true, they qualify.
Premium Membership: Checks if the user is 25+ years old and has an income of $50,000+. If true, they qualify.
Student Discount: Checks if the user is a student. If true, they qualify.
Senior Discount: Checks if the user is 65+ years old. If true, they qualify.
Default Message: If no qualifications are met, it indicates the user does not qualify for any memberships or discounts.
Input:
Age: 20
Annual Income: 1500
Student Status: Yes
Output:
You are eligible for a Student Discount.
Popcorn Hack #2
- Write a program that checks if a person can get a discount based on their age and student status. You can define your own discount criteria! Use compound conditionals to determine the output.
public class Main {
public static void main(String[] args) {
int age = 30; // Change this value for testing
boolean isStudent = true; // Change this value for testing
// Compound conditional logic to determine discount eligibility
if (age < 18 || isStudent) {
System.out.println("You qualify for a discount!");
} else {
System.out.println("You do not qualify for a discount.");
}
}
}
Main.main(null);
You qualify for a discount!