Page 1 of 4

Tip: If/Then in PureBasic!

Posted: Mon Mar 20, 2006 12:36 pm
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!"))

Posted: Mon Mar 20, 2006 3:00 pm
by Rings
thank you for this snippet !

now i can code like a caveman again

THEN !!!!!

Posted: Mon Mar 20, 2006 5:16 pm
by dracflamloc
OH GOD WHY!??!?!?

Posted: Mon Mar 20, 2006 6:23 pm
by Straker
Rings wrote:now i can code like a caveman again
again?

Posted: Mon Mar 20, 2006 6:28 pm
by Bonne_den_kule
Sounds like a batch script for me

Posted: Mon Mar 20, 2006 7:10 pm
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

Posted: Mon Mar 20, 2006 8:08 pm
by thefool
Remember the paranthesis!

Posted: Mon Mar 20, 2006 8:53 pm
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

Posted: Mon Mar 20, 2006 9:04 pm
by PB
>> now i can code like a caveman again
>
> again?

Ohhhh, them's fighting words! :)

Posted: Mon Mar 20, 2006 9:19 pm
by Trond
I've never seen that use of then. Where does that interpretation of what then should do come from?

Posted: Mon Mar 20, 2006 9:34 pm
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. :)

Posted: Mon Mar 20, 2006 10:08 pm
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..

Posted: Tue Mar 21, 2006 9:18 am
by Psychophanta
@PB, Good macro. I like it. :D

Posted: Tue Mar 21, 2006 1:05 pm
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.

Posted: Tue Mar 21, 2006 1:30 pm
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. ;)