@@ -149,9 +149,10 @@ $(GNAME LabeledStatement):
149
149
150
150
$(P
151
151
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.
155
156
)
156
157
$(P
157
158
A label can appear without a following statement at the end of
@@ -1462,62 +1463,124 @@ $(GNAME ContinueStatement):
1462
1463
$(D continue) $(GLINK_LEX Identifier)$(OPT) $(D ;)
1463
1464
)
1464
1465
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.)
1467
1470
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"];
1470
1474
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
1476
1479
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:)
1479
1485
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.)
1482
1497
1483
1498
---
1484
- for (i = 0; i < 10; i++)
1499
+ outer:
1500
+ foreach (item; list)
1485
1501
{
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
+ }
1489
1510
}
1490
1511
---
1491
1512
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
+
1492
1519
$(H2 $(LEGACY_LNAME2 BreakStatement, break-statement, Break Statement))
1493
1520
1494
1521
$(GRAMMAR
1495
1522
$(GNAME BreakStatement):
1496
1523
$(D break) $(GLINK_LEX Identifier)$(OPT) $(D ;)
1497
1524
)
1498
1525
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`)
1500
1527
statement, resuming execution at the statement following it.)
1501
1528
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;
1506
1532
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:)
1509
1547
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.)
1512
1561
1513
1562
---
1514
- for (i = 0; i < 10; i++)
1563
+ // display messages cyclically until the shop is closed
1564
+ outer:
1565
+ while (true)
1515
1566
{
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
+ }
1518
1574
}
1575
+ display("opens at 9am");
1519
1576
---
1520
1577
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
+
1521
1584
$(H2 $(LEGACY_LNAME2 ReturnStatement, return-statement, Return Statement))
1522
1585
1523
1586
$(GRAMMAR
0 commit comments