Rewrite the following code using single if statement.

ICSE Class 10 Computer Applications PYQ · 2025 Specimen · Section A

2(i)1 Mark · 2025 · Section A · 2025 Specimen

Rewrite the following code using single if statement.

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

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.
← Previous questionNext question →

Browse the free Computer Applications sample bank