KnowledgeBoat Logo

Computer Applications

Rewrite the following code using single if statement.

if(code=='g')
System.out.println("GREEN"); 
else if(code=='G') 
System.out.println("GREEN");

Java Conditional Stmts

ICSE Sp 2025

3 Likes

Answer

The code can be rewritten using a single if statement as follows:

if(code == 'g' || code == 'G')
    System.out.println("GREEN");

Explanation:

  • The logical OR operator (||) is used to check if code is either 'g' or 'G'.
  • If either condition is true, the statement System.out.println("GREEN"); will execute. This simplifies the multiple if-else blocks into a single if statement.

Answered By

1 Like


Related Questions