Home > Back-end >  Case statement saying it appeared before
Case statement saying it appeared before

Time:12-02

I have these 2 case statements:

case SDLK_w && SDLK_a:
{
    ball.posX -= mers;
    ball.posY -= mers;
    break;
}

case SDLK_w && SDLK_d:
{
    ball.posX  = mers;
    ball.posY -= mers;
    break;
}

The second SDLK_w gives me an error saying:

case label value has already appeared in this switch

It may be because case statements don't work with &&, but it only shows me an error at the second W key and not at the first one.

Any suggestions?

CodePudding user response:

SDLK_w && SDLK_a is another way of writing 1 because they are both true.
SDLK_w && SDLK_d is also another way of writing 1.

Perhaps you meant this

case SDLK_w:
case SDLK_a: // two case statements in a row: both of them run the same code
{
    ball.posX -= mers;
    ball.posY -= mers;
    break;
}

case SDLK_w:
case SDLK_d:
{
    ball.posX  = mers;
    ball.posY -= mers;
    break;
}

but it still doesn't make sense because SDLK_w has two cases.

  • Related