473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2193
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******@gmai l.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
6514
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
8
5027
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? And if so, provide an example of how its done. Example: int a=23; int b=34;
22
2292
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 initiailised immediately after declaration? Thanks
10
18053
by: Brett Romero | last post by:
If I want to use: switch (AppName) { case ApplicationName.App1: loadApp1Logo(); break; case ApplicationName.App2: loadApp2Logo(); break;
5
2275
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 this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more appropriate forum because the problems I'm having relate to PHP. Any help appreciated. ...
2
1533
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 call routine validatex, the variable retains it's value. But when I call routine validatex from the button click event, variable runLoc is null. How can I retain the value of runLoc permanently? thanks Here's the complete program:
13
2164
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 another calculation then add the result to my retained value etc. my values: Im backing then laying horses. So I back first, say with a return of £20 if it wins. Then I lay the horse with an offset bet giving me an overall return of £1. Ok, so I...
0
817
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: Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) on win32 Type "copyright", "credits" or "license()" for more information.
20
12759
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 AutoExec macro calls a function "InitApplication" which, in turn, calls a function to set the value of a global string variable
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.