Author |
Topic: C++ problem (Read 592 times) |
|
curt_cobain
Newbie
Posts: 33
|
|
C++ problem
« on: Sep 28th, 2008, 1:06pm » |
Quote Modify
|
Following code will give error switch(i) { int x=10; case 1:cout<<"A"; } Following code will node give error switch(i) { int x; case 1:cout<<"A"; } Why??//
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: C++ problem
« Reply #1 on: Sep 28th, 2008, 1:16pm » |
Quote Modify
|
If I had to guess, I would say that declaring variables outside of cases is allowed (but probably a bad habit), whereas changing them isn't.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
0.999...
Full Member
Gender:
Posts: 156
|
|
Re: C++ problem
« Reply #2 on: Sep 28th, 2008, 2:13pm » |
Quote Modify
|
Also odd is the fact that I can easily undermine the system employed by simply doing this (which compiles in my GCC compiler): #include <iostream> using namespace std; int main() { int i = 6; switch(i) { int x; case 6: cout << "Hello world!" << endl; default: x = 1; } return 0; }
|
« Last Edit: Sep 28th, 2008, 2:13pm by 0.999... » |
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: C++ problem
« Reply #3 on: Sep 29th, 2008, 12:36am » |
Quote Modify
|
How is that undermining the system exactly? I can't see any reason why that shouldn't compile; and it's basically an example of Curt's second example which didn't give an error (although it'd be clearer if he had used "not" instead of "node" ).
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
0.999...
Full Member
Gender:
Posts: 156
|
|
Re: C++ problem
« Reply #4 on: Sep 29th, 2008, 4:32pm » |
Quote Modify
|
Disregard that. This works: switch(i) { int x; x = 2; case 6: x = 1; cout << "Hello world!" << endl; } So, it appears that you can't declare and initialize a new variable in the same statement, but you can do anything else with it. Also interesting is that the following prints "Hello, world": switch(i) { i = 2; case 6: cout << "Hello world!" << endl; } Which means that the variable given to the switch is evaluated only once. I declared "i" as volatile to receive the same result.
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: C++ problem
« Reply #5 on: Sep 30th, 2008, 1:13am » |
Quote Modify
|
on Sep 29th, 2008, 4:32pm, 0.999... wrote:Also interesting is that the following prints "Hello, world": switch(i) { i = 2; case 6: cout << "Hello world!" << endl; } Which means that the variable given to the switch is evaluated only once. I declared "i" as volatile to receive the same result. |
| When the program encounters the switch, it immediately jump to the relevant case label, so "i = 2;" is never executed.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: C++ problem
« Reply #6 on: Sep 30th, 2008, 5:07am » |
Quote Modify
|
on Sep 30th, 2008, 1:13am, towr wrote:When the program encounters the switch, it immediately jump to the relevant case label, so "i = 2;" is never executed. |
| Which is also why switch(foo) { int bar = 1; ... } is illegal: by the rules of the language the variable bar must be initialized, but it's in a location where the initialization code will never be executed -- a contradiction. --SMQ
|
|
IP Logged |
--SMQ
|
|
|
|