Tip: If/Then in PureBasic!

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Tip: If/Then in PureBasic!

Post by PB »

Code updated for 5.20+

People have been asking for it... macros have made it possible! :) Enjoy.

Code: Select all

; If/Then macro by PB.  Usage: If <condition> Then (<code>)

Macro Then (code)
  : code : EndIf
EndMacro

age=Val(InputRequester("If/Then example","How old are you?",""))
If age<21 Then (MessageRequester("Result","No beer drinking for you!"))
If age>20 Then (MessageRequester("Result","It's your shout for a beer!"))
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

thank you for this snippet !

now i can code like a caveman again

THEN !!!!!
SPAMINATOR NR.1
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

OH GOD WHY!??!?!?
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Rings wrote:now i can code like a caveman again
again?
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Sounds like a batch script for me
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

I've tried your macro, but without the brackets the message "then is not a valid operator" appears

So I would suggest a workaround which is maybe not that nice (but shorter;)...

Code: Select all

Macro Then
 :
EndMacro 

If a=1 then b=2 : EndIf
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Remember the paranthesis!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> WHY!??!?!?

As I said, because it's been requested here a few times in the past.

> If a=1 then b=2 : EndIf

Having "EndIf" defeats the purpose, and involves more typing than just:

If a=1 : b=2 : EndIf
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

>> now i can code like a caveman again
>
> again?

Ohhhh, them's fighting words! :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I've never seen that use of then. Where does that interpretation of what then should do come from?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I've never seen that use of then

You must be fairly new to Basic, for a comment like that. :)

All the old Basics from the early 80s used this format, as does Visual Basic:

Code: Select all

If <expression> Then <do whatever>
However, PureBasic has implemented it as...

Code: Select all

If <expression> : <do whatever> : EndIf
...which some people here don't like, and they requested it to use the old
Basic way instead. (I can't find the posts now; it's hard to search for "if"
and "then" as keywords as you get too many hits). So this macro gives
them their wish. :)
Last edited by PB on Sat Jul 12, 2014 1:04 pm, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Trond wrote:I've never seen that use of then. Where does that interpretation of what then should do come from?
:shock:

How can you even ask..
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

@PB, Good macro. I like it. :D
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Here's another one:

Code: Select all

Macro Then
  :
EndMacro


caveMan = 1

If caveMan then
  Debug "Still"
Else
  Debug "Once"
EndIf

If caveMan then Debug "Still" : Else : Debug "Once" :EndIf
There is no good reason for "Then" - but nobody told Microsoft to make it optional. I do a lot of ASP and PureBasic coding. Whenever I switch after a biggish session with one, I introduce stupid errors in the other.

End [something] -v- EndSomething.
If exp Then -v- If exp.

etc.

So a Then Macro may mean one less shattered screen or demolished keyboard as frustration (at self) sets in. :)

Ogga Bogga. Wanna see the blunt end of my club? :D




edit: Sorry Michael Vogel, I missed your post with the exact same macro definition (above) - Finally worked out what PB meant (below). lol.
Last edited by Dare2 on Wed Mar 22, 2006 10:31 am, edited 1 time in total.
@}--`--,-- A rose by any other name ..
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@Dare2: You're making the same mistake as Vogel -- having "Then" like that
is just replacing one character (":") with four ("Then"), which is pointless. :)
Your code is bloated for no reason -- it can be done without "Then" like so:

Code: Select all

WITH   : If caveMan then Debug "Still" : Else : Debug "Once" :EndIf
WITHOUT: If caveMan : Debug "Still" : Else : Debug "Once" :EndIf
The purpose of If/Then is to lose the need for having "EndIf" at the end of a
single decision -- nothing more. This was from back in the old days of Basic
when Else did not exist for an If/Then decision. Compare my original code:

Code: Select all

If age<21 : MessageRequester("Result","No beer drinking for you!") : EndIf
If age<21 Then (MessageRequester("Result","No beer drinking for you!"))
It's actually slightly shorter code and, if you're old-school like me, more friendly. :)

Now, that's not to say I'd use this Macro in my own code -- I made it more
of a proof-of-concept thing, to show it can be done. Although, it may make
converting other Basic apps to PureBasic a bit easier, as the coder doesn't
have to sit there and append ":EndIf" to the end of each single decision. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply