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");
}
Java Conditional Stmts
1 Like
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.
Answered By
3 Likes
Related Questions
Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise, display they are equal. The program also displays whether the numbers entered by the user are 'All positive', 'All negative' or 'Mixed numbers'.
Sample Input: 56, -15, 12
Sample Output:
The greatest number is 56
Entered numbers are mixed numbers.Which of the following is not true with regards to a switch statement?
- checks for an equality between the input and the case labels
- supports floating point constants
- break is used to exit from the switch block
- case labels are unique
Write a Java program to print name, purchase amount and final payable amount after discount as per given table:
Purchase Amount Discount upto ₹10000/- 15% ₹10000 to ₹ 20000/- 20% Above ₹20000/- 30% 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.