[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: Nested Switch() statements



PureBytes Links

Trading Reference Links

On Fri, 25 May 2007, Ton Sieverding wrote:
> Can you please explain me why 'switch statements are a frequent
> source of bugs' ? If the last CASE is a DEFAULT then all possible
> cases are covered. Or am I missing something in the AFL SWITCH
> statement ?
>
> switch ( expression )
> {
>     case constant-expression1 : statement;
>     case constant-expression2 : statement;
>     ...
>     case constant-expressionN : statement;
>
>     default : statement;
>
> }
>
>
> Ton.

The 'standard' error when using a switch statement in 'C' and presumably 
AFL, is forgetting to put 'break;' statements in.

To paraphrase your example (above) so it looks like this:-

switch ( expression )
{
    case constant-expression1 : statement1;
    case constant-expression2 : statement2;
    ...
    case constant-expressionN : statementN;
    default : statement_default;
}

If you leave out the break statements, and 
expression==constant_expression1 you will execute statement1 (what you 
wanted) AND ALSO statement2 AND ALSO statement3 AND ALSO ... statementN 
AND ALSO statement_default.

It's sometimes called the 'fall-through' bug.  It's easy to miss, can be 
a right pain!


-- 
	Nigel Rowe
	rho \N{COMMERCIAL AT} swiftdsl \N{FULL STOP} com \N{FULL STOP} au



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/