• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Compiler problem: switch statement

Status
Not open for further replies.

ayeshasalman

New Member
Hi Guys This is Ayesha It has been long time that I'm working on a project of arduino and I have searched my question here and there but couldn’t find it. Well let me explain the issue I'm getting with my arduino switch and its timing.

I believe the arduino that I got from an online electronic store as i think it's SWITCH statement has some compile time problems.

If I try to declare a boolean variable in a CASE statement as illustrated in "case 2: in the snippet below, the compiler throws an error.

I add this to the other CASE statement problem I flagged earlier: ie. the compiler does not throw an error if you misspell "default" as "defalut.")

CODE

switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
boolean X;
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}


This appears to be a problem with the compiler, NOT my code, and it seems to happen only with nested SWITCH statements.

Thank in advance for your prompt response.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
You are not allowed to declare local variables inside a switch statement

If the compiler detects this error it will stop compilation and the default keyword will not be seen..
 

ayeshasalman

New Member
HI Ian Rogers

Thank you so much for your time and knowledge but I would ask if you can please elaborate it more for me. As this is my first project and I won't understand your explanation directly. Thanks.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
C++ can "scope" variables.. What this means is you can declare a variable anywhere.... If you have a while loop and declare a local variable inside the while loop, it will be destroyed after the while loop ends... This means the scope of the variable is fixed to the braces of the statement or function it was declared in.

However! The way the compiler works the for loop and the switch statement are compiled differently and you cannot scope a variable to these statements.... Just declare the "Boolean x; " just before the switch statement and all will be well.. C++ is a bit more complex than most think..

As a long term programmer, I "scope" my variables to the function or global... All my variables are declared BEFORE any code... This way I never run into bother..
 

Pommie

Well-Known Member
Most Helpful Member
BTW, the reason you can put defalut: is that any characters followed by a colon are treated as a label. Hence no error as it's valid code but please don't start using labels and goto.

Mike.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top