Variable retain value between case statements

A

alacrite

If I have code like this:


int main()
{
a=1;
b=2;
x=0;
switch(a)
{
case a:
x=50;
a=b;
break;

case b:
cout<<x; // x=0
break;
}

}

Why will x=0 and not 50 when it is printed in case b? It has scope why
does each case cause it to go back to 0? Is there a way to make the
changes that happen to it in 'a' still be there in 'b'?

thanks,
-Jake
 
R

red floyd

If I have code like this:


int main()
{
a=1;
b=2;
x=0;
switch(a)
{
case a:
x=50;
a=b;
break;

case b:
cout<<x; // x=0
break;
}

}

Why will x=0 and not 50 when it is printed in case b? It has scope why
does each case cause it to go back to 0? Is there a way to make the
changes that happen to it in 'a' still be there in 'b'?

Because your code is not valid. The labels in a case statement must be
an integral constant expression (see 6.4.2/2).

What compiler were you using that accepted this code?
 
S

Scott McPhillips [MVP]

If I have code like this:


int main()
{
a=1;
b=2;
x=0;
switch(a)
{
case a:
x=50;
a=b;
break;

case b:
cout<<x; // x=0
break;
}

}

Why will x=0 and not 50 when it is printed in case b? It has scope why
does each case cause it to go back to 0? Is there a way to make the
changes that happen to it in 'a' still be there in 'b'?

thanks,
-Jake

Only one case is executed. x is not set to 50 unless case a is valid.

You may be further confusing yourself with this ill-formed code. a and
b are variables, but each case statement requires a constant. Execution
goes to only one of the case statements.

switch(a)
{
case 1:
x=50;
a=b;
break;

case 2:
cout<<x; // x=0
break;
}
 
A

alacrite

Scott said:
Only one case is executed. x is not set to 50 unless case a is valid.

You may be further confusing yourself with this ill-formed code. a and
b are variables, but each case statement requires a constant. Execution
goes to only one of the case statements.

switch(a)
{
case 1:
x=50;
a=b;
break;

case 2:
cout<<x; // x=0
break;
}

the same thing happens if 'a' and 'b' are declared using
#define a 1
#define b 2
 
J

Jacek Dziedzic

the same thing happens if 'a' and 'b' are declared using
#define a 1
#define b 2

Yes, obviously, because each time only _one_ of the
branches is taken, not both of them. What is more, if
you #define a to be 1, then you're left with

switch(1)

which makes little sense.

In other words, consult your C++ book on how to use
switch/case, because you've got it way wrong.

HTH,
- J.
 
A

alacrite

Jacek said:
Yes, obviously, because each time only _one_ of the
branches is taken, not both of them. What is more, if
you #define a to be 1, then you're left with

switch(1)

which makes little sense.

In other words, consult your C++ book on how to use
switch/case, because you've got it way wrong.

HTH,
- J.

Yep, sorry I figured out my problem. I forgot to mention that the
switch was in a loop...
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top