How is the condition "Select ... Case?

Post your coding questions such as 'how to' here.
Post Reply
Hacker
Newcomer
Posts: 11
Joined: Mon Aug 11, 2008 1:36 am

How is the condition "Select ... Case?

Post 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.
User avatar
Jare
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori
Contact:

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

Post 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.
User avatar
axu
Devoted Member
Posts: 854
Joined: Tue Sep 18, 2007 6:50 pm

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

Post 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
Jos tämä viesti on kirjoitettu alle 5 min. sitten, päivitä sivu. Se on saattanut jo muuttua :roll:
Image
Hacker
Newcomer
Posts: 11
Joined: Mon Aug 11, 2008 1:36 am

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

Post by Hacker »

Thank You!!!
Post Reply