History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: BOO-988
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Marcus Griep
Reporter: Cedric Vivier
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Boo

optional 'else' block for 'for' and 'while' loops

Created: 08/Apr/08 04:08 AM   Updated: 12/Apr/08 08:06 AM
Component/s: None
Affects Version/s: 0.8.1
Fix Version/s: 0.8.2

Time Tracking:
Not Specified

Issue Links:
Related
Supercedes
 


 Description  « Hide
for <target_list> in <enumerable>:
   <for_block>
else:
   <else_block>

<else_block> is only executed when the loop terminates because <enumerable>.MoveNext() returns false when evaluated, i.e. whenever the <enumerable> attempts to move to the next item, but the <enumerable> is exhausted. This also means that if <enumerable> has no item at the first 'iteration', <enumerable>.MoveNext() will return false, and the <else_block> will be executed.


while <expression>:
     <while_block>
else:
     <else_block>

<else_block> is only executed when the loop terminates the <expression> is tested but resolves to a false, null, or 0 value. This also means that if <expression> is false the first time it is tested (even before entering <while_block>) <else_block> will be executed.

The effect is that for these loops, <else_block> will not be executed when execution leaves the loop due to a break, raise, or return because <expression>/<enumerable>.MoveNext() is not evaluated. Note that the cause of the <else-block> executing is the condition evaluating to false.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Marcus Griep - 08/Apr/08 08:57 AM
This change should be minimally invasive, and as it adds to the language without adversely increasing complexity, I have no problems with it being implemented.

Marcus Griep - 08/Apr/08 01:15 PM - edited
The current flow is equivalent to:
:looptarget
if condition:
  loop_block
  goto looptarget
else:
  else_block
:afterloop

There is discussion going on to change the flow to be equivalent to:

condMet = false
:looptarget
if condition:
  condMet = true
  loop_block
  goto looptarget
elif condMet:
  else_block
:afterloop

where a break in loop_block would evaluate to:

goto afterloop