473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variable retain value between case statements

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
5 2174
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
8
by: Groups User | last post by:
C allows type casting in which a variable is converted from one type to another. Does C (whatever standard) allow the type of a variable to change, within a statement, avoiding the conversion?...
22
by: James Brodie | last post by:
I just wanted to get some advice on what are the better practices: 1) Should variables be declared at the beginning of a function or just before they are required? 2) Should all variabled be...
10
by: Brett Romero | last post by:
If I want to use: switch (AppName) { case ApplicationName.App1: loadApp1Logo(); break; case ApplicationName.App2: loadApp2Logo(); break;
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
2
by: Fred Exley | last post by:
The first time I enter a page, I want to set a variable once, and have that variable retain its value from that point on. So on the first time in the program, I set runLoc = "yes". When I then...
13
by: RobcPettit | last post by:
Hi, after reading up, c# doesnt appear to use gloal variables. Alot of post advise against them also. So Im wondering how to get around this. Im trying to do a calculation, retain the result, do...
0
by: Russell Blau | last post by:
"Support Desk" <support.desk.ipg@gmail.comwrote in message news:01da01c8e1fb$7615a4f0$a701a8c0@office.ipglobal.net... The variable assignment should survive outside the if statements: ...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.