Select Case

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

Select Case

Post by albert »

@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: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Select Case

Post by D.J.Peters »

select case Expression

see here: 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: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Select Case

Post by albert »

@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

Post by sancho3 »

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: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Select Case

Post by grindstone »

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

Re: Select Case

Post by counting_pine »

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: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Select Case

Post by dodicat »

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
 
Post Reply