Content deleted Content added
→Proposed control structures: Wording, loop and a half Tags: Mobile edit Mobile app edit Android app edit App section source |
→Loops: →Loop-and-a-half: New section, stating the problem separately from solutions. Tags: Mobile edit Mobile app edit Android app edit App section source |
||
Line 349:
=== Restart loop ===
Ruby has a <code>retry</code> statement that restarts the entire loop from the initial iteration.<ref>{{cite web|title=control_expressions - Documentation for Ruby 2.3.0|url=https://docs.ruby-lang.org/en/2.3.0/syntax/control_expressions_rdoc.html|access-date=2020-09-25|website=docs.ruby-lang.org}}</ref>
=== Loop-and-a-half ===
Sometimes it is desirable to execute only part of the body on the first or last iteration, especially the beginning or end of the body; this is known as a '''loop-and-a-half'''. General solutions include having a conditional ('''if''' statement) inside the loop, duplicating some of the body before or after the loop, wrapping the half logic in a function, or having a break in the middle. The proposed [[#Loop with test in the middle|loop with test in the middle]] control structure explicitly supports this use case.
=== Early exit from loops ===
|