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
2 Likes
Related Questions
A student executes the following program segment and gets an error. Identify the statement which has an error, correct the same to get the output as WIN.
boolean x = true; switch(x) { case 1: System.out.println("WIN"); break; case 2: System.out.println("LOOSE"); }A triangle is said to be an 'Equable Triangle', if the area of the triangle is equal to its perimeter. Write a program to enter three sides of a triangle. Check and print whether the triangle is equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has its area and perimeter both equal to 30.Which of the following data type cannot be used with switch case construct?
- int
- char
- String
- double
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");