You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New BREAK and CONTINUE statements for controlling FOR and WHILE loop execution #4079
We currently have Exit For Loop (#502) and Continue For Loop (#1125) keywords for this purpose. Having explicit statements would make the usage more clear and closer to how "real" programming languages handle loops. This is especially important when we add WHILE loops (#4084).
When Exit For Loop was added (#502), we decided to use that name instead of Break or Break For Loop because the exiting sounds more natural for non-programmers. That's still a valid point, but I believe these new statements should be named the same way as they commonly are named in other languages. That way they are more familiar for programmers and also people learning basics of programming using Robot's syntax will have it easier to learn other languages later.
See also #4078 about adding somewhat similar RETURN statement.
One obvious thing to decide is what we do with Exit For Loop and Continue For Loop keywords after BREAK and CONTINUE are implemented. I think its better to deprecate and remove them. It's better to have just one way to accomplish one thing and making BuiltIn library smaller is good as well. We should, however, wait at least until RF 5.1 before emitting deprecation warnings.
A related question is what to do with conditional variants Exit For Loop If and Continue For Loop If. They make shorter constructs than IF with BREAK or CONTINUE, but I think having just one way to break/continue is more important. We could consider some kind of inline IF structure later if shorter structures are considered important. It would then work also with RETURN.
Single line IF option would be great. The new IF syntax is soooo much better for conditional blocks than the prior Run Keyword If Run Keywords … AND … AND … AND silliness we had to do. But when you start using the new IF everywhere instead of Run Keyword If , you end up with awkwardness of IF - END’s around single lines all over the place.
I would never choose having to put … and AND stuff for blocks like it was for run keywords Nor would I want anything that only lets you put a keyword that cannot have arguments. But it would be great if IF had two syntaxes:
IF <condition> <keyword> [*args]
IF <condition>
<whatever>
<you>
<want>
END
So if the IF keyword has 3+ arguments, the first syntax is automatically chosen and you can not continue to new lines without using the … continuation keyword, and you may not have more than one keyword (please no AND syntax! 🙂) and you may not nest.
I know this is not how python works but it’s not a completely made up concept either AppleScript (another keyword language) is a good example. They call this “if (simple)” and “if (compound)”. They use the pattern a lot all over too for example Tell works the same way. For a single tell statement on the same line, no end tell is required. But for a block it is required.
IMO that’s the right thing to do, Robot’s roots is a keyword language so even though it’s python based, forcing it to behave with the same pythonic rules (inconveniences) that python has when there is a more natural way to do things will make it too close to python and it looses it’s value prop of simple natural-language-like readability with a focus on telling what’s being tested and not distracting with how the test has to be written.
The above syntax to run single keyword with IF looks pretty good. Would need to think should we also support ELSE and ELSE IF and should we allow assignment:
${var} = IF expr Keyword arg ELSE Another Keyword
This discussion definitely needs its own issue. Could you @ericbjones submit one?
Initial implementation was been done by @bbpatel2001 in already merged PR #4136. Big thanks for contribution!
Things to do:
Add acceptance tests.
Fix using BREAK and CONTINUE with values like BREAK value. That shouldn't be allowed but currently works. This is validated already now by the parser, but the error is not propagated further. Part of this is obviously adding adequate acceptance tests.
Disallowing using BREAK and CONTINUE when not inside FOR or WHILE. Parser doesn't allow using them directly in keyword, but it probably is possible to use wrap them into IF or TRY.
Part of the above is deciding what to do with existing Exit For Loop and Continue For Loop keywords. I believe we should disallow them outside loops as well. That probably should get its own issue. UPDATE: This is covered by Prohibit using Exit For Loop and Continue For Loop in keywords #4185.
Fix using BREAK and CONTINUE inside TRY/EXCEPT. Currently they aren't allowed at all.
Fix minor issues in the PR (mainly whitespace usage).
This ought to be now done except for documentation that is covered by #4203. This issue can now be closed. Big thanks again for @bbpatel2001 for the initial PR!
changed the title [-]New `BREAK` and `CONTINUE` statemens for controlling `FOR` and `WHILE` loop execution[/-][+]New `BREAK` and `CONTINUE` statements for controlling `FOR` and `WHILE` loop execution[/+]on Jan 27, 2022
Activity
pekkaklarck commentedon Sep 6, 2021
One obvious thing to decide is what we do with
Exit For LoopandContinue For Loopkeywords afterBREAKandCONTINUEare implemented. I think its better to deprecate and remove them. It's better to have just one way to accomplish one thing and making BuiltIn library smaller is good as well. We should, however, wait at least until RF 5.1 before emitting deprecation warnings.A related question is what to do with conditional variants
Exit For Loop IfandContinue For Loop If. They make shorter constructs thanIFwithBREAKorCONTINUE, but I think having just one way to break/continue is more important. We could consider some kind of inlineIFstructure later if shorter structures are considered important. It would then work also withRETURN.ericbjones commentedon Sep 9, 2021
Single line IF option would be great. The new IF syntax is soooo much better for conditional blocks than the prior
Run Keyword If Run Keywords … AND … AND … ANDsilliness we had to do. But when you start using the new IF everywhere instead of Run Keyword If , you end up with awkwardness ofIF-END’s around single lines all over the place.I would never choose having to put
…andANDstuff for blocks like it was forrun keywordsNor would I want anything that only lets you put a keyword that cannot have arguments. But it would be great ifIFhad two syntaxes:So if the
IFkeyword has 3+ arguments, the first syntax is automatically chosen and you can not continue to new lines without using the…continuation keyword, and you may not have more than one keyword (please noANDsyntax! 🙂) and you may not nest.I know this is not how python works but it’s not a completely made up concept either AppleScript (another keyword language) is a good example. They call this “if (simple)” and “if (compound)”. They use the pattern a lot all over too for example
Tellworks the same way. For a singletellstatement on the same line, noend tellis required. But for a block it is required.IMO that’s the right thing to do, Robot’s roots is a keyword language so even though it’s python based, forcing it to behave with the same pythonic rules (inconveniences) that python has when there is a more natural way to do things will make it too close to python and it looses it’s value prop of simple natural-language-like readability with a focus on telling what’s being tested and not distracting with how the test has to be written.
pekkaklarck commentedon Sep 10, 2021
The above syntax to run single keyword with IF looks pretty good. Would need to think should we also support ELSE and ELSE IF and should we allow assignment:
This discussion definitely needs its own issue. Could you @ericbjones submit one?
pekkaklarck commentedon Sep 14, 2021
I wrote instructions to #4078 (comment) about how to get started with adding
RETURN. Same instructions apply to this issue as well.robotframework#4079
robotframework#4079
robotframework#4079
robotframework#4079
Implementation continue and break robotframework#4079
Enhance RETURN (#4078)
robotframework#4079
robotframework#4079 CONTINUE and BREAK
8 remaining items
pekkaklarck commentedon Jan 11, 2022
Initial implementation was been done by @bbpatel2001 in already merged PR #4136. Big thanks for contribution!
Things to do:
BREAKandCONTINUEwith values likeBREAK value. That shouldn't be allowed but currently works. This is validated already now by the parser, but the error is not propagated further. Part of this is obviously adding adequate acceptance tests.BREAKandCONTINUEwhen not insideFORorWHILE. Parser doesn't allow using them directly in keyword, but it probably is possible to use wrap them intoIForTRY.Exit For LoopandContinue For Loopkeywords. I believe we should disallow them outside loops as well. That probably should get its own issue. UPDATE: This is covered by Prohibit usingExit For LoopandContinue For Loopin keywords #4185.BREAKandCONTINUEinsideTRY/EXCEPT. Currently they aren't allowed at all.Cleanup
Fix BREAK and CONTINUE in TRY/EXCEPT. Part of #4079.
More unit tests for new control structures
Enhancements and fixes to BREAK and CONTINUE (#4079)
Test handling BREAK and CONTINUE (#4079) and RETURN (#4078) after fai…
Prohibit loop control outside loops.
Rename loop control exceptions
Add `body` to result side RETURN/BREAK/CONTINUE models
pekkaklarck commentedon Jan 23, 2022
This ought to be now done except for documentation that is covered by #4203. This issue can now be closed. Big thanks again for @bbpatel2001 for the initial PR!
[-]New `BREAK` and `CONTINUE` statemens for controlling `FOR` and `WHILE` loop execution[/-][+]New `BREAK` and `CONTINUE` statements for controlling `FOR` and `WHILE` loop execution[/+]