By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

Variable retain value between case statements

P: n/a
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

Oct 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
al******@gmail.com wrote:
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?
Oct 6 '06 #2

P: n/a
al******@gmail.com wrote:
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;
}

--
Scott McPhillips [VC++ MVP]

Oct 6 '06 #3

P: n/a

Scott McPhillips [MVP] wrote:
al******@gmail.com wrote:
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;
}

--
Scott McPhillips [VC++ MVP]
the same thing happens if 'a' and 'b' are declared using
#define a 1
#define b 2

Oct 6 '06 #4

P: n/a
al******@gmail.com wrote:
Scott McPhillips [MVP] wrote:
>>al******@gmail.com wrote:
>>>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;
}

--
Scott McPhillips [VC++ MVP]


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.
Oct 6 '06 #5

P: n/a

Jacek Dziedzic wrote:
al******@gmail.com wrote:
Scott McPhillips [MVP] wrote:
>al******@gmail.com wrote:

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;
}

--
Scott McPhillips [VC++ MVP]

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.
Yep, sorry I figured out my problem. I forgot to mention that the
switch was in a loop...

Oct 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.