Select case/if then elseif 
Author Message
 Select case/if then elseif

Using QBasic/Quick Basic, in applications where either SELECT CASE or
an IF..THEN ..ELSEIF block can be used, is there any compelling reason
to chose one over the other?
--
J.W.Dare



Thu, 20 May 1999 03:00:00 GMT  
 Select case/if then elseif

Quote:

>Using QBasic/Quick Basic, in applications where either SELECT CASE or
>an IF..THEN ..ELSEIF block can be used, is there any compelling reason
>to chose one over the other?

Not compelling reasons, no. It depends largely on personal style and
fitness for purpose. I often but not always use CASE when I want to test
for three or more outcomes and IF when I want to test for true/false.

It's whatever feels right for you IMO. E.g., if I present a menu and ask
the user to press 1..n or Esc, I'd use CASE. If I ask the user if he
wants to quit the program or not, I use IF.

There may be slight performance differences, but I'm not aware of them
and haven't ever needed to know.

--
                   _      N : E : T : A : D : E : L : I : C : A
James Eibisch    ('v')    -- http://www.revolver.demon.co.uk --
Reading, U.K.    (,_,)    --    Now showing: Invaders 1978:  --
                =======   a faithful version of Taito's original



Thu, 20 May 1999 03:00:00 GMT  
 Select case/if then elseif

Quote:


> >Using QBasic/Quick Basic, in applications where either SELECT CASE or
> >an IF..THEN ..ELSEIF block can be used, is there any compelling reason
> >to chose one over the other?

> Not compelling reasons, no. It depends largely on personal style and
> fitness for purpose. I often but not always use CASE when I want to test
> for three or more outcomes and IF when I want to test for true/false.

> It's whatever feels right for you IMO. E.g., if I present a menu and ask
> the user to press 1..n or Esc, I'd use CASE. If I ask the user if he
> wants to quit the program or not, I use IF.

> There may be slight performance differences, but I'm not aware of them
> and haven't ever needed to know.

> --
>                    _      N : E : T : A : D : E : L : I : C : A
> James Eibisch    ('v')    -- http://www.revolver.demon.co.uk --
> Reading, U.K.    (,_,)    --    Now showing: Invaders 1978:  --
>                 =======   a faithful version of Taito's original

  In followup to Eibisch's post, it's largely a matter of taste.

  When dealing with a handful of conditions, it almost makes no
difference whatsoever. The constructions

        IF COND = CONDA THEN                            SELECT CASE COND
          DOSOMETHING                                     CASE CONDA: DOSOMETHING
        ELSEIF COND = CONDB THEN                          CASE CONDB: DOSOMETHINGELSE
          DOSOMETHINGELSE                                 CASE CONDC: DOYETANOTHERTHING
        ELSEIF COND = CONDC THEN                          CASE ELSE:  BRONXCHEER
          DOYETANOTHERTHING                             END SELECT
        ELSE
          BRONXCHEER
        END IF

perform exactly the same functionality (I can't speak to how it
translates in the case of QuickBasic 4.5, however). The reason I'll
generally choose the SELECT CASE construction is purely for readability
for short sets of conditions on which to act.

  If you're dealing with CONDA, CONDB, CONDC ... CONDZ, CONDAA, CONDAB,
... CONDAZ, CONDBA, CONDBB ... CONDZZ, the underlying translator still
doesn't care (assuming there's no limit to the number of cases in a
SELECT CASE list) one way or the other, but readibility gets to be an
issue. With the format of the second example above, finding an error if
you have to debug such a monstrosity is easier than having to wade
through barnyardfuls of IF ... THEN... ELSEIF... ENDIF constructions.



Thu, 20 May 1999 03:00:00 GMT  
 Select case/if then elseif

-10 points for politically UNcorrectness!!!!!

CALL CompellingReason( 9 )

Sub CompellingReason ( Index as Integer)

  Select Case Index%
    Case 0 : Print "It is a lot neater than IF...THEN...ELSEIF block"
    Case 1 : Print "easier to read"
    Case 2 : Print "easier to debug"
    Case 3 : Print "easier to understand"
    Case 4 : Print "easier to write"
    Case 5 : Print "It is the trend"
    Case 6 : Print "It will make you more money"
    Case 7 : Print "you computer will run faster"
    Case 8 : Print "your computer will never break down'
    Case 9 : Print "Persons of the oposite sex will find you
attractive"
  End Select

End Sub

  ____    _    ____      ____  _____
 |  _ \  / \  / ___) __ | ___)(_   _) |  Don Schullian
 | |_)  / _ \ \____\/  \|  _)   | |   |    Gramou 33
 |____//_/ \_\(____/\__/|_|     |_|   |  Papagou, 15669
 ___________________________________  |      Greece
  =================================== |  ++30-1-654-6200

   -----------======### DOWNLOAD ###=========------------
            Nutz 'n Boltz for powerbasic v3.2  
              A library of everyday goodies
   DOWNLOAD N-B-V2??.ZIP FROM YOUR FAVORITE SITE TODAY!



Fri, 21 May 1999 03:00:00 GMT  
 Select case/if then elseif

Quote:

> Using QBasic/Quick Basic, in applications where either SELECT CASE or
> an IF..THEN ..ELSEIF block can be used, is there any compelling reason
> to chose one over the other?

Personally I use CASE whenever I'm testing the same variable for multiple
ranges.  ie:

if x => 1 and x <= 5 then
  do whatever
elseif x > 5 and x <= 10 then
  do different junk
etc....

I like

select case x
case 1 to 5
case 5 to 10
etc..

just makes it for less typing, and neater overall in cases likes this.
also use it all the time for menus....   eric



Wed, 26 May 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Select Case Vs If - ElseIf

2. If..Elseif vs. Select case

3. VBScript Select Case vs VB Select Case

4. "select...case", multiples cases not possible ?

5. Select Case and Case 0 To 9

6. Select case ... OR If...Elseif... ??

7. Academic Question - Select Case vs. If ElseIf - which is better

8. Select Case module

9. Select case using NOT operator

10. Select case Zero

11. Help with Select Case...again

12. Select Case Statement Help

 

 
Powered by phpBB® Forum Software