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 » Thu Aug 06, 2009 3:25 am

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
Moderator
Moderator
Posts: 860
Joined: Mon Aug 27, 2007 10:18 pm
Location: Helsinki

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

Post by Jare » Thu Aug 06, 2009 4:04 am

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 » Thu Aug 06, 2009 11:56 am

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 » Fri Aug 07, 2009 2:53 am

Thank You!!!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest