Page 232 - ComputerScience_Class_11
P. 232
11. Define conditional statements in Java. How does the flow of control work with ‘if’, ‘if-else’ and ‘switch case’ statements? Illustrate
with examples.
Ans. Conditional statements control the flow based on certain conditions.
if Statement: Executes if the condition is true.
For example,
if (a > b) { System.out.println(a + " is greater"); }
if-else Statement: Executes one block if true, another if false. Example:
if (a == b) { System.out.println("Equal"); } else { System.out.println("Not equal"); }
switch Statement: Selects one of many blocks of code to execute based on the value of a variable. Example:
switch (a) { case 1: System.out.println("One"); break; default: System.out.println("Invalid"); }
12. Explain how ‘if-else-if’ statements function. How does it differ from the ‘if’ statement and when is it used? Provide an example.
Ans. if-else-if checks multiple conditions sequentially and executes the corresponding block when a condition is true. Unlike a simple
if statement that checks one condition, if-else-if checks several.
For example,
if(a>=90)
System.out.println("Grade A");
else
if(a>=80)
System.out.println("Grade B");
else
System.out.println("Grade C");
System.out.println("Grade Obtained");
D. Higher Order Thinking Skills (HOTS)
1. Imagine you are building an e-commerce system where customers can apply different discount coupons, but some coupons have
multiple conditions. Write a pseudo-code using nested if-else statements that ensures coupons are applied correctly based on the
combination of the total order value and customer loyalty points. How would the system handle cases where multiple conditions
are met?
Ans. A nested if-else structure can be used to check first for the total order value and then for loyalty points within each condition:
if(orderValue > 5000) {
if(loyaltyPoints >= 1000) {
apply "Gold Member Discount";
} else {
apply "Standard Discount";
}
} else if(orderValue > 2000) {
if(loyaltyPoints >= 500) {
apply "Silver Member Discount";
} else {
apply "Base Discount";
}
} else {
apply "No Discount";
}
The system needs to ensure that if multiple conditions are met (for example, high order value and high loyalty points), the highest
applicable discount is applied.
230 Touchpad Computer Science (Ver. 3.0)-XI

