View previous topic :: View next topic |
Author |
Message |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Sep 30th, 2013, 6:45pm Post subject: Experiment - While...Then...Else |
|
|
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 |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Oct 5th, 2013, 9:37pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 6th, 2013, 3:49pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 10th, 2013, 5:10pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 11th, 2013, 6:25pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Oct 12th, 2013, 6:52am Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 12th, 2013, 2:37pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 13th, 2013, 3:00am Post subject: Re: Experiment - While...Then...Else |
|
|
I just updated the article along with several files in the root and src. _________________ Brent |
|
Back to top |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Oct 15th, 2013, 10:44pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Nov 5th, 2013, 9:30pm Post subject: Re: Experiment - While...Then...Else |
|
|
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 |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Nov 5th, 2013, 10:14pm Post subject: Re: Experiment - While...Then...Else |
|
|
I can fully support your concern and solution  _________________ Stefan
Any code I post can be freely used, just give credit. |
|
Back to top |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
|
Back to top |
|
 |
|