Page 1 of 2
if i=1 then b=5
Posted: Sat Jun 19, 2004 10:21 pm
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

Posted: Sat Jun 19, 2004 10:29 pm
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.
Posted: Sun Jun 20, 2004 4:06 pm
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.
Posted: Sun Jun 20, 2004 4:22 pm
by PolyVector
I would kill for this!....grrr...stupid colons!
Posted: Mon Jun 21, 2004 12:08 pm
by DoubleDutch
if would make code a lot neater and easier to read (for single line if's)
good idea

Posted: Mon Jun 21, 2004 12:52 pm
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.
Posted: Mon Jun 21, 2004 3:03 pm
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?
Posted: Mon Jun 21, 2004 3:13 pm
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
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):)
Posted: Mon Jun 21, 2004 3:44 pm
by blueznl
else if (with a space in between) is not such a good idea, lots of people would read it as:
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
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...)
Posted: Tue Jun 22, 2004 8:49 pm
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.
Posted: Tue Jun 22, 2004 9:39 pm
by GPI
allow it since 1980

You found so many thing in PureBasic which are not standard.
Posted: Tue Jun 22, 2004 11:16 pm
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

Posted: Wed Jun 23, 2004 10:16 am
by Jose
on the question of "I miss this" IF
Yes, a simple function to write, but nice to have it coded and optimized in ASM....

Posted: Wed Jun 23, 2004 6:30 pm
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.
Posted: Wed Jun 23, 2004 9:52 pm
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 !!!