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.

Newcomers DBPro Corner / Multiple IFs

Author
Message
Stavdav
17
Years of Service
User Offline
Joined: 4th May 2007
Location:
Posted: 6th May 2007 14:45
I'm stupid, how could I shorten:



?

Please help I'm stupid.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th May 2007 17:18 Edited at: 6th May 2007 17:18
Something like this perhaps:



Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th May 2007 18:57
And using Goto is not recommended. Use procedures and Gosub, or functions instead.

With Goto, your programs will soon turn into a tangled mess.

TDK_Man

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th May 2007 19:28
I agree. I just recently got used to putting everything into functions (everything apart from UDTs, and the call for the first function, heh). I wish DB had a more C(++) style of coding

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th May 2007 20:26 Edited at: 6th May 2007 20:27
Quote: "With Goto, your programs will soon turn into a tangled mess."

Yes, if using goto for everything. However, there is one use for goto:



The point of using goto here is to execute code that you don't want to create copies of for each failed function call, while keeping the function self-contained. And of course another purpose of this is to automatically unload something automatically at the end of the function.

I saw code similar to this on MSDN, and thought "what the hell?". Of course when you think about it, it is perfectly logical.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 6th May 2007 22:15
Quote: "Of course when you think about it, it is perfectly logical."


What exactly about it is logical? There's no need to have conditonals, that unload code will execute 100% of the time.

"Once there was a polygon mesh who was very sad because he was only Gouraud shaded."
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th May 2007 22:32 Edited at: 6th May 2007 22:47


No. When something fails, it goes straight to the end of the function to unload anything. If it didn't, it would continue trying to load stuff after it, which is pointless as the function cannot now proceed. The unload code is supposed to execute 100% of the time, so that even if the function fails at some point it can free any resources it allocated without having to add code to do so at every operation.

See this more practical example:



Note that the second operation doesn't have to be loading another file (don't start thinking about using a loop for it), it can be any other kind of operation.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 6th May 2007 23:11
@Stavdav

Another way to shorten the code would be:



I would also recommend using gosub instead of goto.

LB
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th May 2007 23:27 Edited at: 6th May 2007 23:27
Strange, I didn't think of that! In C++ I would have.

However, I would like to note that (joystick fire x(j) && 1) = 1 is comparing 1(or 0) with 1, so you can just write it as (joystick fire x(j) && 1).

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 7th May 2007 03:01
goto noto

Stavdav
17
Years of Service
User Offline
Joined: 4th May 2007
Location:
Posted: 7th May 2007 12:35
Thanks for all the replies. I know I need to use functions, the only programming I've done was Actionscripting in Flash where I was always using functions. When I started with Darkbasic I'm not sure why I didn't start using them straight away.

I used



So I didn't need the second condition but alway code will be very useful. I got the program to where I wanted it for now but the code is laughable in parts.

Thanks again

I'll be back.

Please help I'm stupid.

Login to post a reply

Server time is: 2024-11-25 18:02:28
Your offset time is: 2024-11-25 18:02:28