Computer Applications
Rewrite the following code using single if else statement:
if (brd.equals("ICSE")) {
if (code == 86) {
System.out.println("ICSE Computer Applications");
}
else {
System.out.println("Unknown subject");
}
}
else {
System.out.println("Unknown subject");
}
Answer
if (brd.equals("ICSE") && code == 86) {
System.out.println("ICSE Computer Applications");
} else {
System.out.println("Unknown subject");
}
Reason — The original code had nested if-else conditions, where "Unknown subject" was printed in two places. By using logical AND (&&), the conditions are combined into a single if statement. This removes unnecessary nesting and ensures the same logic in a single if-else statement.
Related Questions
The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return
An air-conditioned bus charges fare from the passengers based on the distance travelled as per the tariff given below:
Distance Travelled Fare Up to 10 km Fixed charge ₹80 11 km to 20 km ₹6/km 21 km to 30 km ₹5/km 31 km and above ₹4/km Design a program to input distance travelled by the passenger. Calculate and display the fare to be paid.
Differentiate between if else if and switch-case statements