if i=1 then b=5

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Kazmirzak
User
User
Posts: 92
Joined: Fri Jun 18, 2004 5:44 pm
Location: Germany

if i=1 then b=5

Post by Kazmirzak »

I really miss the good and old 'then' for single line - if -then...

:-(

...
if i=3 then b=5 else b=0
if i=4 then b=4 else b=3
...

Maybe it isn't really important but would be very nice ;-)
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

you could always use a colon. like this:

If i=3:b=5:EndIf

that works, and its single lined.
the : works like a line seperater.
Kazmirzak
User
User
Posts: 92
Joined: Fri Jun 18, 2004 5:44 pm
Location: Germany

Post by Kazmirzak »

Sure, that's possible, but it looks much more like 'basic' to write

If i>1 then b=5 else b=-5

than

If i>1 : b = 5 : else : b=-5 : end if

Wouldn't it be (at least) possible to implement, that the compiler simply replaces every "then" with ":"!? Yes, I know that this is not on the top of the todo-list, but many coders of other basics are used to the 'then' - Pure Basic would make a step to more flexibility.
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

I would kill for this!....grrr...stupid colons!
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

if would make code a lot neater and easier to read (for single line if's)

good idea :)
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

There's a reason for it not to use it:
You'll ran into the so-called 'dangeling else' - problem: If there's no termination of an optional Else-part, there's no chance for the compiler to make a difference between:

Code: Select all

If a=1 : If b=1 : z = 3 : Else : z = 4 : EndIf : Endif
and

Code: Select all

If a=1 : If b=1 : z = 3 : Endif : Else : z = 4 : EndIf
because it would both look like:

Code: Select all

If a=1 : If b=1 : z = 3 : Else : z = 4
(or with 'THEN')

Code: Select all

If a=1 Then If b=1 Then z = 3 Else z = 4
The only way would be a non-syntax-standard to define e.g. that an 'Else' always refers to the first possible 'If' left of it, but that would do things more complicated, or you could have to use brackets around any Else-part, but that's not BASIC.

But perhaps as an optional extension, well, hmmm, ... , no I personally don't want it.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

but a simple:
If <expression> then <statement>:<statement> [else <statement>:<statement>]

after the eol then it would be automatically endif

would work and it would make programs look easier to read/neater

it may be quite easy to add a check to see if the 1st info after the "if <expression>" is a "then", if it is then auto-endif after the eol? Fred?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hmmm.

Of

Code: Select all

If exp
  dosome : thing
else
  dosome
  thingelse
endif
-v-

Code: Select all

If exp : dosome : thing : else : dosome : thingelse : endif
-v-

Code: Select all

If exp then dosome : thing  else  dosome : thingelse
I prefer the first, then the second. The colons clarify things.

No objection to change though, if the then and eol approach is an optional approach. Then everyone to their own.

Now an optional then as in

Code: Select all

If exp then
  dosomething
endif
would mean no errors after you've been coding in some other basic and your fingers are now in automatic-then mode. Could there be a case for Else If and End If here? (j/k):)
@}--`--,-- A rose by any other name ..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

else if (with a space in between) is not such a good idea, lots of people would read it as:

Code: Select all

if
  ...
else
  ...
if ... then ... would be okay with me, as the 'then' keyword indicates the resulting acion / condition takes place before the next eol / colon, at which point the regular flow continues

Code: Select all

if ... then ...
though i don't see it as an essential, and it can cloudify :-) coding, as in the following two examples

Code: Select all

if a=1 then a=2 : a=3

if a=1 : a = 2 : endif : a=3
in general, i don't like the colons at all, to be honest... i'm of the 'one statement per line' school

(although lately i admit i'd like to spread a line of code over more than one line of text...)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Kazmirzak
User
User
Posts: 92
Joined: Fri Jun 18, 2004 5:44 pm
Location: Germany

Post by Kazmirzak »

OK, let's make a summary.

1. Following has to be possible:

Code: Select all

if i=3 : if j=4 : <command> : endif : else : <comands> : endif
2. Following has to be possible

Code: Select all

if i=3 then <commands> else <commands> endif
if i=3 then if j=4 then <commands> endif else <commands> endif
I agree that it is impossible to allow the leaving out of "endif", because this would be really confunsing (you'd think it is multiline??), but it should be possible to leave out the colons [:], like other interpreters (Blitz Basic, Power Basic ...) allow it since 1980 ;-) I think that is a very good compromise, which should be possible to implement without any serious change in the logic.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

allow it since 1980 ;-)
You found so many thing in PureBasic which are not standard.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

@GPI
perhaps you could implement a customizeable preprocessor inside jaPBe (with saveable configuration)
Then we could define our own complex find/replace-rules, e.g. 'then' becomes ':' or think of even more-complex macros. E.g. we could write 'Global peter.s = "test"' and it will be compiled to 'Global peter.s : peter.s = "test"'

Ok - it would be better if PB itself would have such a precompiler :wink:
%1>>1+1*1/1-1!1|1&1<<$1=1
Jose
User
User
Posts: 34
Joined: Sat Apr 26, 2003 9:20 pm

Post by Jose »

on the question of "I miss this" IF

Code: Select all

iif(<exp>, true , false)
Yes, a simple function to write, but nice to have it coded and optimized in ASM.... :lol:
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Im shocked, apparently Im the only one so far that doesn't want IF THEN statements. DarkBasic, which I have used for a few years, has them, and I learned to program very sloppily with IF THEN. I would have things like

if x>500 then gosub dowhatever : thisfunction() : health=health-512 : color=rnd(255) : cls color : print "you lose" : goto startagain

and I would end up with really long sloppy lines. Then I started using Purebasic, and at first I didnt like the lack of THEN but now I am glad it isnt there, it keeps my programming much more clean and actually easier to read in my opinion.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

kenmo,

I am neither shocked nor glad, but I love the way PureBasic does ...

You are not alone.

If somebody wants something like Then Then maybe Fred will try to solve this But (maybe a missing keyword in PureBasic) I will NOT use it because I also don't like multiple commands in single lines.

Allright BTW : try this !

Then = #TRUE

If Then
dothis()
Else
dothat()
EndIf

That's it !!!
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Post Reply