Select Case

General FreeBASIC programming questions.
albert
Posts: 4588
Joined: Sep 28, 2006 2:41
Location: California, USA

Select Case

Postby albert » Jan 04, 2019 0:22

@Coders

FB "select case" doesn't work with commas like QB

Code: Select all


screen 19

dim as longint a = int(rnd*2)
dim as longint b = int(rnd*2)

select case  a , b
    case 0 , 1 : print "a="; a
    case 1 , 0 : print "b=" ; b
    case 1 , 1 : print "ab = " ; a+b
end select

sleep
end



Could you coders make it work like QB
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: Select Case

Postby D.J.Peters » Jan 04, 2019 1:46

select case Expression

see here: /wiki/KeyPgSelectcase

(a,b) isn't a legal expression

Joshy

Code: Select all

screen 19
randomize timer()
var a = int(rnd*2)
var b = int(rnd*2)

select case  b*2+a
  case 0 : print "ab = 0"
  case 1 : print "a  = 1 "; a
  case 2 : print "b  = 1 "; b
  case 3 : print "ab = 1" ; a , b
end select

sleep
albert
Posts: 4588
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Select Case

Postby albert » Jan 04, 2019 2:10

@DJPeters

My above code , works with QB , but not with FB...
QB allows for comma separated lists in the select case statement , FB doesn't.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Select Case

Postby sancho3 » Jan 04, 2019 2:28

Where are you getting a QB that works with that code?
I cannot find any documented select case that incorporates a comma separated expression.

In FB the case expression can be comma separated. So how would the comma separated select expression even work

Code: Select all

dim x = 12, y = 1
Select Case x
   Case 1, 2, 3, 4:
      Print "hello"
   Case 12,16,17:
      pirnt "world"
End Select

Select Case x, y      ' what would be the outcome here?
   Case 1, 2, 3, 4:
      Print "hello"
   Case 12,16,17:
      pirnt "world"
End Select
grindstone
Posts: 640
Joined: May 05, 2015 5:35
Location: Germany

Re: Select Case

Postby grindstone » Jan 04, 2019 11:21

My QB 1.1 definitely does not work with comma separated SELECT CASE.
counting_pine
Site Admin
Posts: 6155
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Select Case

Postby counting_pine » Jan 04, 2019 14:11

QB/FB has always supported multiple choices in a Case statement, but never multiple expressions in a Select Case block. It would get very confusing if both were possible.

I guess the simplest workaround would be something like: select case (a & "," & b) (where a is numeric, or at least doesn't contain a comma).

For small enough numbers (i.e. where there's no overflow risk), you could do a * N + b, where N (preferably a power of 2) is known to be greater than b.
dodicat
Posts: 5655
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Select Case

Postby dodicat » Jan 04, 2019 16:02

Use a dummy case.

Code: Select all



do
    cls
    locate 2
randomize
dim as longint a = int(rnd*2)
dim as longint b = int(rnd*2)
print "a = ";a;"  ";"b  =  ";b
select case  1=1
    case a=0 and b=1 : print "a="; a
    case a=1 and b=0 : print "b=" ; b
    case a=1 and b=1 : print "none zero"
    case a=0 and b=0 : print "both zero"
end select

sleep
loop  until inkey=chr(27)
end
 

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest