ICSE Class 10 Computer Applications Question 21 of 30

Solved 2025 Specimen Paper ICSE Class 10 Computer Applications — Question 21

Back to all questions
21
Question

Question 2(i)

Rewrite the following code using single if statement.

if(code=='g')
System.out.println("GREEN"); 
else if(code=='G') 
System.out.println("GREEN");
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.