Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / If statements

Author
Message
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 22nd Jan 2011 05:59 Edited at: 22nd Jan 2011 06:05
I've only just remembered about how 'if' statements work after seeing Lee's usage of it in an example, I think they should be simplified and be made more legible:

In DBPro the following are the same:



I think it would increase readability if the two concepts were merged, so it would become(assuming the required brackets):



I think requiring the 'then' makes the if statement look more like written English, and removing the ability to do the exact same thing multiple ways means a beginner doesn't have to learn as much. If you notice, the difference between my two 'if' statements is the removal of the carriage returns, in DBPro a completely different syntax is required.

In addition to this, the 'elseIf' statement should be added, i.e.



What does everyone think?

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 22nd Jan 2011 06:04
I'd rather operation stacking was removed to be honest. As you say, if you stack multiple operations in an IF and also do the same with ELSE there can be ambiguities (not to mention the fact that the code is hard to read!). Since we can do IF blocks there isn't really a need to stack operations on the same line.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 22nd Jan 2011 06:07
But then the 20 line competition wouldn't quite have as good entries

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Jan 2011 12:31
Single-line 'if' statement
My personal preference would be to remove the multi-command syntax completely (ie, the colon command separator), leave the 'then' form of the command as it is except for removing the 'else' clause from it.

Multi-line 'if' statement
Leave it as it is, except for the addition of 'elseif' sections.

Quote: "But then the 20 line competition wouldn't quite have as good entries"

So? Since when has packing as much into as few lines as possible been a language design criteria (except for Perl )

bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 22nd Jan 2011 12:45 Edited at: 22nd Jan 2011 13:20
Eh? Leave colons, I want to be able to stack certain commands.

It's up to the programmer to use it.

[edit]: While i'm not particularly against elseif, I think switch statements are clearer.

original: I don't think we need elseif. Switch statements are clearer than elseif.

I realized right after printing it that my statement was flawed

[center]
Join the TGC Group!
http://tehcodez.groups.live.com
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Jan 2011 12:48
Quote: "I don't think we need elseif. Switch statements are clearer than elseif."


But each has a completely different function...

I'd like to see you try to do this with a switch/select statement:



I definitely agree with elseif, it makes some code alot easier to read.

[b]
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Jan 2011 12:52 Edited at: 22nd Jan 2011 12:53
Except that in DBPro, they don't do the same thing:


With SELECT, there's one expression and it's matched against a list of constants. With IF/ELSEIF, there are multiple, potentially unconnected, expression checks.

[edit]Doh! Beaten to it.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Jan 2011 12:58 Edited at: 22nd Jan 2011 12:59
I think I agree with Dark Coder.

I've always found the present if/then/else/endif arrangement messy and confusing.

Why should successive statements on one line be handled any differently from successive statements on separate lines?

I would expect the following to be equivalent:



and



with similar versions using the elseif keyword.

The " then" is of course unnecessary in those two examples but does aid readability. Perhaps make it optional? Note that grouping operators aren't needed since the grouping is implied by the if/else/endif keywords.

Quote: "My personal preference would be to remove the multi-command syntax completely (ie, the colon command separator),"


I'd be happy with that - but if we keep it let's implement it consistently.

Quote: "Since when has packing as much into as few lines as possible been a language design criteria"


How about now?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 22nd Jan 2011 12:59
If the ability to stack multiple lines into one is removed, then indeed, adding the 'then' would be redundant. I would personally like to see the stacking ability removed, because all it does is make code harder to read.

And like other things in DBPro, it provides multiple syntaxes to do exactly the same thing, this should be stamped out.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Jan 2011 13:07
Quote: " I would personally like to see the stacking ability removed, because all it does is make code harder to read."


Depends. Sometimes it's helpful to have as much code as possible on screen at the same time, in which case having several short statements grouped together on one line can be helpful, especially if several similar groups are involved.

The following is what I have in mind:



However, I could certainly live without it. It would be a backward step though with no real gain.
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Jan 2011 13:10
I quite like how you can do this though:


Perhaps it could be made so that "then" lets you only use a single command? This would in fact be more in-keeping with BASIC style.

I wouldn't mind being able to do this either:


[b]
bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 22nd Jan 2011 13:17 Edited at: 22nd Jan 2011 13:18
Quote: "
"


What's wrong with:



Yeah, ok. With elseif the procedure can be shortened up, I'm in favor.

[center]
Join the TGC Group!
http://tehcodez.groups.live.com
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Jan 2011 13:35
Yeah, it's not so much a problem when it's that short, but often you might have many more of these and the indentation level becomes unmanageable.

[b]
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 22nd Jan 2011 21:41
You can sneak some ideas from C-style syntax disguised as Basic for this 'elseif' discussion.

As in C, a statement can be either a single statement (ignore the fact that I used the word in it's own definition), or a block of multiple statements. Then, an if statement only takes a single statement after it, and that statement may in turn be a block of multiple statements. The same can be said for 'else', so the 'if' in 'else if' is considered a single statement (which contains in itself other statements).

So, I would propose syntax like this: a single statement after an if would look like this:



A single statement in an if statement just follows the 'then' on the same line.

To make it a block of multiple statements, it would look like this:


And the same applies to 'else'.

So 'elseif' would now look like this:




which is internally treated like this, at the most abstract level:




Gettit?

"everyone forgets a semi-colon sometimes." - Phaelax
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Jan 2011 22:41 Edited at: 22nd Jan 2011 22:42
So basically, if the statement after the "then" or "else" is omitted, it becomes a multi-line block.

I quite like that syntax. The only slight problem I can see is that statements like this do not have the "endif" which people might object to:



Whereas this version would have an endif:


[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2011 01:47
Returning to my earlier question, why should multiple statements per line require a different syntax from multiple lines (other than the necessary statement/end of line delimiters of course)?
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 02:23
They don't with this method.

[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2011 11:54
Good. Then does it differ from my earlier suggestion?
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 12:35 Edited at: 23rd Jan 2011 13:05
These are the key points about it:
- If we have a stacking character it would behave identically to a newline character
- The syntax for an if statement is as follows:
Quote: "if <condition> then <statement>"

OR:
Quote: "if <condition> then
<statements>
endif"

- The syntax for an else is similar:
Quote: "else <statement>"

OR:
Quote: "else
<statements>
endif"


Essentially, if you put a statement on the same line as an IF or ELSE then it doesn't require an ENDIF, and an optional ELSE can appear on the line following it. If the statement immediately after the THEN is the start of a block, then the entire block is in the IF statement.

So these are all valid pieces of code:


As you can see it lets you do all sorts of things! Some of which are extremely useful.

[b]
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jan 2011 12:58 Edited at: 23rd Jan 2011 12:59
The fact that you had to comment this, means it's confusing.


I'd use that confusion as justification for the removal of command stacking, and just use the : symbol as a marker for labels.

The reason that 'THEN' is currently a keyword (although technically unnecessary) is that it signals a different type of IF statement that doesn't need an ENDIF. In the DBPro compiler, it's used as a switch to go down differing paths for that reason.

The reason 'THEN' is included *despite* being unnecessary (IMO) is that it makes that differing path explicit in the code.

The shortcut evaluation you've suggested is a byproduct of how the parsing works right now. BTW, according to your specifications, your code is incorrect:

(unless 'IF' is followed by 'THEN', except where it isn't is the rule )

TBH, I'm not sure why everyone is trying to redesign the IF statement. It's pretty clear right now - THEN for single-line, no THEN for multiline.

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 13:12
Fixed.

I wasn't arguing either way about command stacking, just saying that if it was included, it should behave like a new-line, instead of having different behaviours in an IF statement.

The reason we're trying to design it is this:
Although then/no then works fine for the IF part of an IF statement, there is no alternative for the ELSE part.

DBPro actually already supports blocks in a single line IF statement which I was suprised about:


I'm simply expanding it so that the same can be done with the ELSE statement, allowing the "ELSE IF" syntax, among other things.

[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2011 13:21
Quote: "I'd use that confusion as justification for the removal of command stacking, and just use the : symbol as a marker for labels."


I'd use it as a reason for going with my earlier suggestion. Why have two potentially confusing versions of the syntax?

Just treat stacked commands as equivalent to successive commands and the problems go away.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jan 2011 13:23
Quote: "Although then/no then works fine for the IF part of an IF statement, there is no alternative for the ELSE part."

Sorry, but I'm being obtuse this morning and I've obviously missed something, but I don't know what you mean by this.

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 13:32 Edited at: 23rd Jan 2011 13:32
There are two versions of the IF block, a single line and a multiline one, but in DBPro the else always takes the same form as the if statement.


OR


So it is actually possible to use "else if" syntax in DBPro:


But only with everything on a single line, which is useless.

What I'd like to be able to do is choose whether each part is single line or multiline, so for an ELSE IF, the first IF would be multiline, the ELSE would be single line but be the start of an IF block, and so on, allowing you to have things like this:


[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2011 15:59
Why? What's the advantage of that over this:



or the corresponding version on a single line:



[Note that I'm using : to indicate a line break.]
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 16:20 Edited at: 23rd Jan 2011 16:49
So what would the syntax be when using "if ... then"?
(ie. Can you use an else, and if so how?)

[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2011 20:12
I would just have the "then" as an optional keyword after the condition as suggested in my earlier post. As someone pointed out it helps readability which may be useful for novices. It looks best in the multi-line form but it would be a shame to force people to do that as I pointed out before.

My experience on the forum is that the old DBPro style if/then caused more problems than it solved because it was inconsistent with the other syntax. So I agree with those who suggested getting rid of it altogether. The essential syntax, on one or more lines, would be something like



(I've omitted the recursive extension of that syntax for simplicity).

In that snippet optional parts are indicated by [] and choices by |. The else/elseIf chain would either be absent or one of the following



or




or



or



etc.

So an "elseIf" ,if used, would have to be the first and similarly an "else" would have to be the last.

I could write out the whole syntax in correct recursive style but I guess you get the idea.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 23rd Jan 2011 23:31
Using the syntax that Diggsey and I are talking about is simpler because it's what follows the 'else' that's important. In other words, you could just as easily do this:



This is consistent with 'else if'. Your method would have to have an 'elsewhile' and 'elsefor' and 'elsedo', etc, unless you just wrap them all in a big 'else'/'endif', which only takes us back to square 1.

"everyone forgets a semi-colon sometimes." - Phaelax
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Jan 2011 23:38
We could also make the "then" optional, so DBPro block style IF statements would still work with no changes.

[b]
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Jan 2011 00:30
Would you honestly use code like that? An ELSE terminated visually by and ENDWHILE?

I won't go so far as to call that an abomination, but it's not far off

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 24th Jan 2011 00:56
Fine I just thought it would be a nice feature. I don't really mind, just as long as we have some way to do Else If!

[b]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2011 11:25
Quote: "I don't really mind, just as long as we have some way to do Else If!"


Same here.
Alquerian
17
Years of Service
User Offline
Joined: 29th Mar 2006
Location: Reno Nevada
Posted: 17th Feb 2011 01:51
I would suggest removing the 'then' keyword and replacing it with { and a closing } for 'end'/'endif'. Some editors (such as eclipse) allow you to double click within the {}, selecting it all, and making it easier to see the opening and closing of a logic statement.

I have no qualms about inline if statements or block if statements, they are both fine. I have mixed feelings about short-hand if-statements such as:
LeeBamber
TGC Lead Developer
24
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 18th Mar 2011 03:26
Hi Guys,

Quite a lot of thoughts on the IF THEN question. To dumb it down for me a little, it seems most of us want an ELSEIF command added so we can do things like this:

IF A=1
B=2
ELSEIF A=2
B=3
ELSEIF A=3
B=4
ENDIF

There also seems to be some debate over the inconsistencies between the single and multi-line IF statements. They where always going to be different as one came from the legacy BASIC syntax and the other came from a C style syntax. I think there is place in the universe for both styles, and I personally use both for different purposes. I did pick up a vibe of unrest regarding the use of ELSE within the single command variant though, i.e:

IF A=1 THEN B=2 ELSE B=3

As you can see, this is nice and read to read. But what if you wanted to stack, you had to do this:

IF A=1 : B=2 : C=22 : ELSE : B=3 : C=33 : ENDIF

Essentially because IF THEN ELSE was not designed with stacking in mind. Any subsequent success with stacking in the single line IF statement was purely accidental. One solution I can suggest is that command stacking will produce a compiler error within a single line IF statement, i.e:

IF A=1 THEN B=2 : C=3

Or any variation of this that includes THEN would produce an error. This should limit the confusion caused by the successive victories when using THEN and stacking in strange combinations.

I personally use command stacking ALL THE TIME in DBP to keep my code from stretching into the future. For example, FPSC source is about 45,000 lines of code. Without stacking it would easily be one quarter of a million lines of code!

So my suggestion for this thread; (a) add ELSEIF and (b) compiler error if you stack on the same line as a THEN command. Sound ok?

I drink tea, and in my spare time I write software.
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 18th Mar 2011 09:07
Lee:
OK!


Don't care about THEN and stacking. But I really love ELSEIF

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 18th Mar 2011 15:31
Agreed as far as it goes, but how do you see the ELSE statement combining with the THEN keyword? Is it completely eliminated in that scenario or not?

LeeBamber
TGC Lead Developer
24
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 7th Apr 2011 04:59
I see an ELSE that follows a THEN as a quick way of defining a single outcome from a condition. Anything more complex should use IF and ENDIF. The compiler will (should) trap any attempt to squeeze lots of commands into the THEN and ELSE code. A typical usage would be:

IF A=1 THEN B=2 ELSE B=3

Nice and simple. Something like this:

IF A=1 THEN B=2 : C=3 : ELSE : B=3 : C=4

Or variations on that theme begin to get confusing and rules blur. Might be best to ban the : symbol from the THEN ELSE line as traditionally it was a much simpler construct than DBP eventually allowed.

I drink tea, and in my spare time I write software.

Login to post a reply

Server time is: 2024-03-28 21:44:50
Your offset time is: 2024-03-28 21:44:50