> Brace Requirements:
>
> I have been using Switch/Case statements forever. But I found an
> example that makes me wonder if I have misunderstood something.
>
> I get a compile error with the below code if I don't use braces as
> shown in the 'case' statement. I know it's because of theChr
> declaration but I don't understand why or how the braces fixes the
> problem. I have never used braces before in case statements.
>
> What's up?
>
> Error: parse error before theChr
>
> case keyDownEvent: { // Without this brace and matching closing brace
> // I get compile errors on the next statement.
> Char theChr = event->data.keyDown.chr;
>
> handled = true;
> switch (theChr) {
> case '\n':
> RtnButtonPressed(); // Initiate calculation
> SetFieldFocus(fldXReg);
> break;
> default: handled = false; // not yet handled
> break;
> } // end switch(theChr)
> break;
> } // this and it's matching brace required else error on theChr
> // declaration.
It's because C requires you to place all variable definitions for a
given scope before any executable code within that scope. By using the
braces, you are creating a new scope.
--
Roger Chaplin
<[EMAIL PROTECTED]>