531,859 views
12 votes
12 votes
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 :

User LouieGeetoo
by
2.7k points

1 Answer

21 votes
21 votes

Answer:

A: cases can't have a test condition

Step-by-step 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

}

User Whj
by
3.4k points