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
1 Like
Related Questions
Which of the following data type cannot be used with switch case construct?
- int
- char
- String
- double
Using the switch-case statement, write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
Letters Unicode A 65 B 66 . . . . . . Z 90 (b) Display the following pattern using iteration (looping) statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Differentiate between if else if and switch-case statements