Page 1 of 1

How is the condition "Select ... Case?

Posted: Thu Aug 06, 2009 3:25 am
by Hacker
Hi:

Anybody could explain me how to use the condition "Select ... Case?

Could you give me a small sample code please?

Many thank you very much. Adios.

Re: How is the condition "Select ... Case?

Posted: Thu Aug 06, 2009 4:04 am
by Jare

Code: Select all

variable = 3
Select variable
Case 1
	Print "It's one"
Case 2
	Print "It's two"
Case 3
	Print "It's three"
Case 4
	Print "It's four"
Default
	Print "It's something Else than 1, 2, 3 Or 4"
EndSelect
WaitKey
Select can also take an expression instead of 'variable', but Case always needs a constant value. You can also use other data types in Case - such as string. Some languages have a Break command to separate Cases, but CoolBasic does not have it - all cases are separated automatically.

Re: How is the condition "Select ... Case?

Posted: Thu Aug 06, 2009 11:56 am
by axu
And you can use many parameters within one Case, using ',' as separator.

Example with strings and multiple choises in one Case:

Code: Select all

Dim variable As String
variable = "B"
Select Lower(variable)      //Make the comparation use only lowercase letters
    Case "coolbasic"
       Print "It's CB!"
    Case "a", "b", "c", "d"
       Print "It's a, b, c or d"
    Default
       Print "It's not anything I know"
EndSelect
WaitKey
By the way, Constants wont work with Select...Case, does anyone know why :( ? Try it:

Code: Select all

Const Constant = 2
variable = 2
Select variable
    Case Constant
       Print "It's " + Constant
    Default
       Print "It's not " + Constant
EndSelect
WaitKey

Re: How is the condition "Select ... Case?

Posted: Fri Aug 07, 2009 2:53 am
by Hacker
Thank You!!!