comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Bug in SWITCH - ELSE: statement?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Bug in SWITCH - ELSE: statement? [message #56232 is a reply to message #56231] Fri, 12 October 2007 12:43 Go to previous messageGo to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Thu, 11 Oct 2007 09:44:27 +0000, Spon wrote:

> Hi everyone,
>
> can someone explain this to me please?
>
>> [quoted text muted]
> The ELSE clause of the SWITCH statement is optional. If included, it
> matches any selector expression, causing its code to be executed. For
> this reason, it is usually written as the last clause in the switch
> statement. **The ELSE statement is executed only if none of the
> preceding statement expressions match.** If an ELSE clause is not
> included and none of the values match the selector, program execution
> continues immediately below the SWITCH without executing any of the
> SWITCH statements.
>
> But, for example:
>
> PRO SWITCHTEST
>
> N = FIX (6 * RANDOMU (S, 1) ) + 1
>
> SWITCH N OF
> 1: PRINT, 'One'
> 2: PRINT, 'Two or less'
> 3: PRINT, 'Three or less'
> 4: PRINT, 'Four or less'
> 5: PRINT, 'Five or less'
> 6: PRINT, 'Six or less'
> ELSE: PRINT, 'Are you using loaded dice?'
> ENDSWITCH
>
> PRINT, N
> END

You probably wanted CASE, which has an implicit BREAK after each
statement. Or put the BREAKs in yourself:

1: begin
print, 'One'
break
end

SWITCH is really useful only when you want to "fall through" to multiple
processing steps. If you do want to fall through, you must BREAK
somewhere before ELSE:

SWITCH N OF
1: PRINT, 'One'
2: PRINT, 'Two or less'
3: PRINT, 'Three or less'
4: PRINT, 'Four or less'
5: PRINT, 'Five or less'
6: begin
PRINT, 'Six or less'
break
end
ELSE: PRINT, 'Are you using loaded dice?'
ENDSWITCH

Yes, it's ugly.

JD
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: structure into strcuture
Next Topic: How to make an attribute of the oject visible outside of the class

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 07:47:57 PDT 2025

Total time taken to generate the page: 0.39650 seconds