If ... Then

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
diceman
User
User
Posts: 34
Joined: Tue Apr 10, 2018 9:42 pm

If ... Then

Post by diceman »

One petty thing I'm missing in PureBasic, now don't laugh, is the "If ... Then" Statement.
When there's just one line of consequence, "If ... Then" looks cleaner, plus you'll get another tool to structure your code.

Code: Select all

If condition : doStuff : Endif
is clunky.
Last edited by diceman on Mon Jun 17, 2019 12:37 pm, edited 1 time in total.
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: If ... Then

Post by BarryG »

User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: If ... Then

Post by RSBasic »

Why not with : ?

Or you create a macro:

Code: Select all

Macro Then
  :
EndMacro
Image
Image
User avatar
diceman
User
User
Posts: 34
Joined: Tue Apr 10, 2018 9:42 pm

Re: If ... Then

Post by diceman »

RSBasic wrote:Why not with : ?

Or you create a macro:

Code: Select all

Macro Then
  :
EndMacro
But you'd still need the Endif at the end. And that's what makes it clunky (imho). :P
But I like the trick in Barry's link quite a bit:

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!"))
That really works? Looks to good to be true! I mean, I've worked with Macros before, but not to that extent ...
I gotta try that when I'm at home!
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: If ... Then

Post by oreopa »

If...Then is a hangover from basic with line numbers IMO. Most (or at least C64 basic V2) couldnt do multiline statements, so THEN was a kinda logical thing (remember "ON A GOTO 100,200,300...."? :D Thats how I used to handle multiple cases.)

These days I don't see the use of it... it seems like verbosity for verbosities sake, but I'm glad you kinda got the solution anyway ;) Macros FTW!
Proud supporter of PB! * Musician * C64/6502 Freak
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: If ... Then

Post by Little John »

diceman wrote:One petty thing I'm missing in PureBasic, now don't laugh, is the "If ... Then" Statement.
When there's just one line of consequence, "If ... Then" looks cleaner, plus you'll get another tool to structure your code.
Code where If sometimes is followed by Then, and sometimes is not followed by Then, does not look cleaner to me than the code which is currently valid in PureBasic, where If is never followed by Then.

-1 from me.
User avatar
diceman
User
User
Posts: 34
Joined: Tue Apr 10, 2018 9:42 pm

Re: If ... Then

Post by diceman »

Thanks again for pointing me to the location where I got my solution from. It might not seem like a big thing, but you just made me the 2nd happy man in the world (No.1 happiest man is the guy married to Kate Upton).
You see, I'm just a humble hobbyist who has never never coded with anything else than BASIC-Languages, and I'm so used to using "Then" with single line consequences, I immediately noticed it's absence when switching from Blitz to Pure (even though I'm in love with all the rest PB has to offer). Might be an OCD-Thing, even, can't rule that out for sure. :lol:
Here's my final solution (the capital letters make it stick out as a command, and the underscore seperates it visually from the Statement):

Code: Select all

Macro THEN_(code)
	: code : EndIf
EndMacro
An appropriate Thank You:
Image
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: If ... Then

Post by BarryG »

You're not related to Kwai chang caine by any chance? LOL! :lol:
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: If ... Then

Post by Fangbeast »

He can't be. Kwai would have posted an animated clip of the Hoff in his speedos that none of us could ever unsee!
Amateur Radio, D-STAR/VK3HAF
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: If ... Then

Post by RSBasic »

:lol:
Image
Image
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: If ... Then

Post by HanPBF »

When I see such a statement in code (Basic, Pascal, etc.):

Code: Select all

;pseudo code
if a = b then c = 1
I first immediately change it to

Code: Select all

;pseudo code
if a = b then
  c = 1
endif
and second kick the coder!
procedure.s iifs(check.b, whenTrue.s, whenFalse.s)
if check
procedureReturn whenTrue
else
procedureReturn whenFalse
endif
endProcedure

define a.s = iifs(bool(1 = 0), "true", "false")
User avatar
diceman
User
User
Posts: 34
Joined: Tue Apr 10, 2018 9:42 pm

Re: If ... Then

Post by diceman »

Btw. ...
I'm back to If / Else / Endif ... :mrgreen:

Image

You guys are right, "Then" is unneccessary and turns beautiful, readable code into noisy garbage. I learned that the hard way.
I was so sure that I was missing something, but when I finally got it, it turned out to be a major disappointment and not at all how I remembered it.
It's been such a long time since I last did something with another language than PB, and in the meantime, it seems like PureBasic has turned me into a better human being. :) :idea:
Yet, this thread was not in vain, since I learned something new about Macros, how to further use and abuse them.
Thanks a lot, guys!
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: If ... Then

Post by Tenaja »

I used to use macros to make pb more like what I was accustomed to, back when I first started. I quit doing it before a year had passed. By the third year, I looked at some of my original code and wondered what the heck I was thinking. I look back and know I should have just embraced the language as it was for what it was.
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: If ... Then

Post by blueb »

Tenaja wrote:...I should have just embraced the language as it was for what it was.
Amen brother... Amen. :mrgreen:
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: If ... Then

Post by Marc56us »

Tenaja wrote:I used to use macros to make pb more like what I was accustomed to, back when I first started. I quit doing it before a year had passed. By the third year, I looked at some of my original code and wondered what the heck I was thinking. I look back and know I should have just embraced the language as it was for what it was.
That's also why I even avoid creating my own libs and modules for small basic functions and prefer to use the PB model system or keyboard automatic typer.
Code sometimes less readable at the time but much more years later.
And EXE is not less or more sized
:)
Post Reply