Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Experiment - While...Then...Else

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Sep 30th, 2013, 6:45pm    Post subject: Experiment - While...Then...Else Reply with quote

Suppose you wrote some code like the following:
Code:
While aFunctionWithSideEffects()
    ' whatever
Wend
But then you need to know whether the loop body actually got executed. One way would be
Code:
executed = false
While aFunctionWithSideEffects()
    executed = true
    ' whatever
Wend
If executed Then
   ' whatever
Else
    ' whatever
End If
or
Code:
If aFunctionWithSideEffects() Then
    Do
        ' whatever
    Loop While aFunctionWithSideEffects()
    ' whatever
Else
    ' whatever
End If

Neither solution is very satisfactory. The first needs a flag to be set to false before the loop, then it has to set that flag to true for every iteration of the loop. The second avoids that (slight) overhead but adds complexity by calling the function in two locations and requires the programmer to keep them in sync any time a test condition needs changing.

I'm thinking of modifying the syntax of the While loop to add "Then" and "Else" clauses.
Code:
' Proposal 1
While aFunctionWithSideEffects()
    ' whatever
Then
    ' whatever
Else
    ' whatever
Wend
However, I wonder if someone seeing this for the first time would be confused about which blocks of code were repeated and which were not.

An alternative might be to severely limit the "Then" and "Else" clauses to simple statements only, or even just a single assignment statement.
Code:
' Proposal 2
While aFunctionWithSideEffects()
    ' whatever
Then executed = true
Else executed = false
Wend
If executed Then
   ' whatever
Else
    ' whatever
End If

Comments?

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Oct 5th, 2013, 9:37pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

In my source codes, I have not seen any evidence where this could be useful, since I never use a flag to indicate the execution of a loop.

I think the implementation of this is to be based on parser complexity and implementation complexity in the language.

The syntax itself is clear.

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 6th, 2013, 3:49pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

Stefan, I think this situation arises most frequently when data is read from a file into a data structure (eg an array), then processed, and only if the data in memory was modified does it get written back to disk.

Anyway, I plan to add these clauses to most types of loops where they make sense. All types should support "Then," and all types except the "Do...Loop While/Until" loop should support "Else."

I added a section to the following article, and the concept should have an article of its own soon.
http://www.b6sw.com/ViviFire/docs/Loop_Structures.html

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 10th, 2013, 5:10pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

So, I just found the time to work on this. The source and executable files are updated and available from the usual place.
http://www.b6sw.com/ViviFire

Also, I'll be starting on the help article today.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 11th, 2013, 6:25pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

I just updated the main article, http://www.b6sw.com/ViviFire/docs/Then_Else_Clauses.html

Please take a look and maybe tell me what you think. I'm open to any suggestions, even suggestions to completely redo it, or even eliminate it.

Edit: renamed file

_________________
Brent


Last edited by Brent on Oct 15th, 2013, 10:29pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Oct 12th, 2013, 6:52am    Post subject: Re: Experiment - While...Then...Else Reply with quote

Will the THEN block be executed when EXIT {Loop} will be used too?

If I want to end the loop early using EXIT it might be beneficial to not execute the THEN block, so I have a difference to meeting the loops condition.

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 12th, 2013, 2:37pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

STPendl wrote:
If I want to end the loop early using EXIT it might be beneficial to not execute the THEN block, so I have a difference to meeting the loops condition.

Yes, that is the behavior I plan to implement. I'll be adding that to the "Remarks" section soon.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 13th, 2013, 3:00am    Post subject: Re: Experiment - While...Then...Else Reply with quote

I just updated the article along with several files in the root and src.
_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Oct 15th, 2013, 10:44pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

I just renamed several files, including the article of concern to this thread, and so have edited the two links posted previously.

I also accidentally erased the database that "newest.php" uses, so it's back to reporting all diffs as zeroes until I next update a file.

BTW, I am still updating the .CHM file about weekly, generally Sunday or Monday.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Nov 5th, 2013, 9:30pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

I've been looking at these constructions for a while and I'm thinking that "Then" and "Else" don't stand out enough. If no one objects, I'm strongly considering changing these keywords to "Afterward" and "Otherwise", respectively.
Code:
While condition
    ' loop body
Afterward
    ' success
Otherwise
    ' failure
End While

Some of the alternatives I considered were:
  • Then
    Additionally After Afterward Afterwards Lastly Subsequently Thereafter
  • Else
    Instead Otherwise

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Nov 5th, 2013, 10:14pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

I can fully support your concern and solution Wink
_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Nov 6th, 2013, 10:48pm    Post subject: Re: Experiment - While...Then...Else Reply with quote

The changes are complete. The relevant article is now at the following address:

http://www.b6sw.com/ViviFire/docs/Afterward_Otherwise_Clauses.html

I've also updated the parser (sources and binary) and help file.

http://www.b6sw.com/ViviFire

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group