Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / SELECT / CASE oddness

Author
Message
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 19th Jan 2008 22:01


I thought the code would check what A is at the point where you do SELECT, then run CASE 1, then ignore CASE 2 even if A = 2 at that point, because the SELECT was made when A = 1. It doesn't. This code returns A = 3. Is that correct or an error in the code?


A mod has been erased by your signature because it was larger than 600x120
Link102
20
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 19th Jan 2008 22:22
the help files say
Quote: "ENDCASE
Use this command in combination with SELECT, CASE and ENDSELECT to create a select statement. A caseend statment specifies the end of a piece of code you are specifying for a case statement. "


if you want to fix this just count backwards:


TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Jan 2008 22:24
I would say that 3 is the correct value to expect.

However, it's not the way that it's supposed to be used - a bit like the problems you would get if you altered the variable A in the middle of a For A = 1 To 100 loop.

TDK_Man

Sinani201
18
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 19th Jan 2008 22:34
The SELECT and CASE command set is like using the IF command.
I wrote a tutorial on it on TDK's Game Programming Forums.

Seriously, how do you make the little blue text come up below your message?
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 19th Jan 2008 22:35
Hmm, so I was wrong to expect SELECT/CASE to work differently than IF/THEN statements? Whats the advantage in SELECT statements, then? I could've written this as:



I thought SELECT's power was that it would decide the variable to be used at the point where you use SELECT, not at the points where you use CASE.


A mod has been erased by your signature because it was larger than 600x120
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Jan 2008 23:07
Quote: "IF A = 1
A = A + 1
ENDIF
IF A = 2
A = A + 1
ENDIF"


or

If A=1 Or A=2
IA = A + 1
Endif


or

If A=1 Or A=2 Then Inc A

and so on...

The only advantage to Select..Case is that it is a lot neater than lots of If..Then statements. To be honest, DB's Select Case isn't a fully implemented version, so it could be a lot better.

In fact, it was only added because it was chosen by the winner of a competition to choose a new command.

Can't remember who it was though...

TDK_Man

tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 19th Jan 2008 23:28
Lol, that explains it. My actual code is a lot complexer, the A = A + 1 thing was only an example. If A=1 Or A=2 Then Inc A would be much better in this case .

Well, I've fixed the bug in my program and lost an illusion of SELECT having a special use.


A mod has been erased by your signature because it was larger than 600x120
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 19th Jan 2008 23:48
I really like select case but I would like it to be more powerful, It seems to get confused when you start using <> etc

I Didn't know it was a competition winner's choice haha
If I win I will have them add a MAKE OBJECT PANDA command

Sinani201
18
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 20th Jan 2008 00:12
In blender, you can add a Monkey head by pressing space>ADD>MESH>Monkey, which is similar to your Panda command

Seriously, how do you make the little blue text come up below your message?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Jan 2008 01:55
@tha_rami

The select case command set works very similarly to the switch case command set in C. There is another command that is optional to add to the switch - case commands called break. In C, your program would behave exactly the same as it is in DBC unless the break command followed the end of each case condition. The break forces execution to jump out of the switch command all together after the case condition is met - otherwise, the condition of the next case is tested. Using the dreaded goto in the select satement, you can mimic the behaviour of your own C-like break command. In fact, you might call it jump or something to avoid a conflict with the real DBC break command (which pauses execution).



Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 20th Jan 2008 02:20
@Latch
wouldn't EXIT have the same effect?

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Jan 2008 02:44
I thought it might, but it kicks back a nesting error. I think EXIT only applies to loops.

Enjoy your day.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 20th Jan 2008 06:05 Edited at: 20th Jan 2008 06:18
Odd. I would expect it to break out of the control structure straight after executing the matching case. In DBP it works like this (and thus the code results in a value of 2).

This means it must check every case even after it finds the right one, which is surely inefficient.

Quote: "In C, your program would behave exactly the same as it is in DBC unless the break command followed the end of each case condition."

It wouldn't actually. The only reason it's entering the second case is because it's checking the expression straight after finishing a case block, as if it is simply a series of IF statements (to my knowledge it should be IF/ELSEIF).

In C, case statements can run straight into each other; it doesn't matter if the next case value is different. The expression check comes before any cases.

tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 20th Jan 2008 07:04
Quote: "The expression check comes before any cases."

That's what I expected it to do, indeed. Not that case, it appears.


A mod has been erased by your signature because it was larger than 600x120
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Jan 2008 09:01
Quote: "Quote: "In C, your program would behave exactly the same as it is in DBC unless the break command followed the end of each case condition."
It wouldn't actually"


Thanks for catching that! I meant the result would be the same because it is adding a+1 each case. If the breaks weren't there (in C) regardless of the case value,

Quote: "the condition of the next case is tested"

should read: the next case expression is evaluated.

Quote: "This means it must check every case even after it finds the right one, which is surely inefficient."


I was trying to put some reason to how Select and Case work in DBC by comparing it to how you can run multiple cases together in C but it seems DBC's just doesn't work right.

Enjoy your day.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 20th Jan 2008 09:19 Edited at: 20th Jan 2008 09:34
Quote: "I meant the result would be the same because it is adding a+1 each case."

Ah yes of course.

Quote: "If the breaks weren't there (in C) regardless of the case value,

Quote: "the condition of the next case is tested"
should read: the next case expression is evaluated."

I'm not sure what you mean by this. This is the BASIC equivalent of a C switch statement:



This is what it appears DBC does:



I'd imagine this is how DBP does it:



Login to post a reply

Server time is: 2025-06-02 10:20:38
Your offset time is: 2025-06-02 10:20:38