Skip to content

Commit cf839f9

Browse files
authored
[spec] Improve break and continue docs (#3576)
1 parent 2be77bb commit cf839f9

File tree

1 file changed

+95
-32
lines changed

1 file changed

+95
-32
lines changed

spec/statement.dd

Lines changed: 95 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ $(GNAME LabeledStatement):
149149

150150
$(P
151151
Any statement can be labeled, including empty statements,
152-
and so can serve as the target
153-
of a goto statement. Labeled statements can also serve as the
154-
target of a break or continue statement.
152+
and so can serve as the target of a $(RELATIVE_LINK2 goto-statement, goto statement).
153+
Labeled statements can also serve as the
154+
target of a $(RELATIVE_LINK2 break-statement, break) or
155+
$(RELATIVE_LINK2 continue-statement, continue) statement.
155156
)
156157
$(P
157158
A label can appear without a following statement at the end of
@@ -1462,62 +1463,124 @@ $(GNAME ContinueStatement):
14621463
$(D continue) $(GLINK_LEX Identifier)$(OPT) $(D ;)
14631464
)
14641465

1465-
$(P `continue` aborts the current iteration of its enclosing loop statement, and
1466-
starts the next iteration.)
1466+
$(P `continue` aborts the current iteration of its innermost enclosing
1467+
loop statement, and starts the next iteration.
1468+
If the enclosing loop is a `for` statement,
1469+
its $(GLINK Increment) clause is executed.)
14671470

1468-
$(P continue executes the next iteration of its innermost enclosing
1469-
while, for, foreach, or do loop. The increment clause is executed.)
1471+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1472+
---
1473+
string[] words = ["OK", "just", "longer", "words", "now"];
14701474

1471-
$(P If continue is followed by $(I Identifier), the $(I Identifier)
1472-
must be the label of an enclosing while, for, or do
1473-
loop, and the next iteration of that loop is executed.
1474-
It is an error if
1475-
there is no such statement.)
1475+
foreach (w; words)
1476+
{
1477+
if (w.length < 4)
1478+
continue; // skip writeln
14761479

1477-
$(P Any intervening finally clauses are executed, and any intervening
1478-
synchronization objects are released.)
1480+
writeln(w);
1481+
}
1482+
---
1483+
)
1484+
$(P Output:)
14791485

1480-
$(P $(D Note:) If a finally clause executes a throw out of the finally
1481-
clause, the continue target is never reached.)
1486+
$(CONSOLE
1487+
just
1488+
longer
1489+
words
1490+
)
1491+
1492+
$(P If `continue` is followed by $(I Identifier), the $(I Identifier)
1493+
must be the label of an enclosing loop statement,
1494+
and the next iteration of that loop is executed.
1495+
It is an error if
1496+
there is no such statement.)
14821497

14831498
---
1484-
for (i = 0; i < 10; i++)
1499+
outer:
1500+
foreach (item; list)
14851501
{
1486-
if (foo(i))
1487-
continue;
1488-
bar();
1502+
// try 3 times
1503+
foreach (i; 0 .. 3)
1504+
{
1505+
if (item.buy())
1506+
continue outer; // skip to next item
1507+
1508+
log("attempt failed");
1509+
}
14891510
}
14901511
---
14911512

1513+
$(P Any intervening $(RELATIVE_LINK2 try-statement, `finally`) clauses are executed,
1514+
and any intervening synchronization objects are released.)
1515+
1516+
$(P $(D Note:) If a `finally` clause executes a `throw` out of the finally
1517+
clause, the continue target is never reached.)
1518+
14921519
$(H2 $(LEGACY_LNAME2 BreakStatement, break-statement, Break Statement))
14931520

14941521
$(GRAMMAR
14951522
$(GNAME BreakStatement):
14961523
$(D break) $(GLINK_LEX Identifier)$(OPT) $(D ;)
14971524
)
14981525

1499-
$(P `break` exits the innermost enclosing while, for, foreach, do, or switch
1526+
$(P `break` exits the innermost enclosing loop or $(RELATIVE_LINK2 switch-statement, `switch`)
15001527
statement, resuming execution at the statement following it.)
15011528

1502-
$(P If break is followed by $(I Identifier), the $(I Identifier)
1503-
must be the label of an enclosing while, for, do or switch
1504-
statement, and that statement is exited. It is an error if
1505-
there is no such statement.)
1529+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1530+
---
1531+
const n = 55;
15061532

1507-
$(P Any intervening finally clauses are executed, and any intervening
1508-
synchronization objects are released.)
1533+
// find the smallest factor of n
1534+
foreach (i; 2 .. n)
1535+
{
1536+
writeln("Trying: ", i);
1537+
if (n % i == 0)
1538+
{
1539+
writeln("smallest factor is ", i);
1540+
break; // stop looking
1541+
}
1542+
}
1543+
writeln("finished");
1544+
---
1545+
)
1546+
$(P Output:)
15091547

1510-
$(P $(D Note:) If a finally clause executes a throw out of the finally
1511-
clause, the break target is never reached.)
1548+
$(CONSOLE
1549+
Trying: 2
1550+
Trying: 3
1551+
Trying: 4
1552+
Trying: 5
1553+
smallest factor is 5
1554+
finished
1555+
)
1556+
1557+
$(P If `break` is followed by $(I Identifier), the $(I Identifier)
1558+
must be the label of an enclosing loop or `switch`
1559+
statement, and that statement is exited. It is an error if
1560+
there is no such statement.)
15121561

15131562
---
1514-
for (i = 0; i < 10; i++)
1563+
// display messages cyclically until the shop is closed
1564+
outer:
1565+
while (true)
15151566
{
1516-
if (foo(i))
1517-
break;
1567+
foreach (msg; messages)
1568+
{
1569+
if (shop.isClosed())
1570+
break outer; // end the while loop
1571+
1572+
display(msg);
1573+
}
15181574
}
1575+
display("opens at 9am");
15191576
---
15201577

1578+
$(P Any intervening $(RELATIVE_LINK2 try-statement, `finally`) clauses are executed,
1579+
and any intervening synchronization objects are released.)
1580+
1581+
$(P $(D Note:) If a `finally` clause executes a `throw` out of the finally
1582+
clause, the break target is never reached.)
1583+
15211584
$(H2 $(LEGACY_LNAME2 ReturnStatement, return-statement, Return Statement))
15221585

15231586
$(GRAMMAR

0 commit comments

Comments
 (0)