Westonci.ca connects you with experts who provide insightful answers to your questions. Join us today and start learning! Join our platform to connect with experts ready to provide detailed answers to your questions in various areas. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts.

What is wrong with this case statement -> case (x > 2):
A. cases can't have a test condition
B. cases must be capitalized
C. cases must use a ; and not a :


Sagot :

Answer:

A: cases can't have a test condition

Explanation:

Under the hood, switch statements don't exist. During the mid-stage of compilation, a part of the compiler will lower the code into something that is easier to bind. This means that switch statements become a bunch of if statements.

A case in a switch statement acts upon the switch value. Think of the case keyword as the value you pass into the switch header:

int x = 10;

switch (x)

{

case (x > 2):

     // Code

     break;

}

// Becomes

if (x(x > 2))

{

// Code

}

// Instead do:

switch (x)

{

case > 2:

     // Code

     break;

}

// Becomes

if (x > 2)

{

// Code

}