declaring variables in switch statement

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

Is it not possible to declare variables within a switch statement? My
variables don't seem to ever be in scope to be able to assign values to
them.


Thanks,

Mike
 
Mike P said:
Is it not possible to declare variables within a switch statement? My
variables don't seem to ever be in scope to be able to assign values to
them.

Could you provide an example showing your problem? It'll help someone answer
you(I won't be here long enough, unfortunatly).
 
Hi Mike,

You can declare variables inside a switch statement, but it will go out of scope as the statement goes out of scope.
If you want to use the variables after the switch you need to declare them before the switch, with a default value.

int j = -1;

switch(myValue)
{
case 0:
j = 200;
break;
}

int k = j*3;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top