29.2k views
4 votes
The following if statement determines whether choice is equal to 'Y' or 'y':

if (choice == 'Y' || choice == 'y')
Simplify this statement by using either the toupper or tolower function.
A) if (toupper(choice) == 'Y')
B) if (tolower(choice) == 'y')
C) if (toupper(choice) == 'Y' || toupper(choice) == 'y')
D) if (tolower(choice) == 'y' || tolower(choice) == 'y')

1 Answer

6 votes

Final answer:

To simplify the original if statement (if (choice == 'Y' || choice == 'y')), one can use the toupper or tolower function to convert 'choice' to a uniform case for comparison, resulting either in 'if (toupper(choice) == 'Y')' or 'if (tolower(choice) == 'y')'.

Step-by-step explanation:

The if statement in question checks if the variable 'choice' is equal to 'Y' or 'y'. This can be simplified by using either the toupper or tolower function to make the character uppercase or lowercase, respectively, and then comparing it with a single character. The correct simplified statement would:

A) if (toupper(choice) == 'Y')

or

B) if (tolower(choice) == 'y')

Both of these options will correctly check if 'choice' is 'Y' or 'y' regardless of its case because they convert 'choice' to a single case before the comparison. Option C is not a simplification since it still includes two comparisons, and Option D is incorrect because it redundantly checks the same condition twice.

User Grethel
by
8.5k points