We've talked about adding the title to the filename several times over the years and it seems really valuable. This requires us to compute a "slug" for the title spelling that can be part of the filename. Beyond that, we crossed 7000 recently, and so it seems likely that we will need to add digits sooner rather than later here, so this goes ahead and moves us to 6 digits so we don't have to adjust again for a reasonable length of time. To implement this and ensure we can sustain it going forward this adds a tool to our pre-commit that validates (and corrects if needed) the filename. In order to update everything and keep links working, there are a _lot_ of changes, but the most interesting for direct review are in `proposals/scripts`. Assisted-by: Antigravity with Gemini --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
3.3 KiB
Loops
Table of contents
Overview
Carbon provides loops using the while and for statements. Within a loop, the
break and continue statements can be used for flow control.
Details
while
while statements loop for as long as the passed expression returns True.
Syntax is:
while (boolean expression) {statements}
For example, this prints 0, 1, 2, then Done!:
var x: Int = 0;
while (x < 3) {
Print(x);
++x;
}
Print("Done!");
for
for statements support range-based looping, typically over containers. Syntax
is:
for (var declarationinexpression) {statements}
For example, this prints all names in names:
for (var name: String in names) {
Print(name);
}
PrintNames() prints each String in the names List in iteration order.
TODO: Add semantics discussion from
#1885: ranged-based for for user-defined types.
break
The break statement immediately ends a while or for loop. Execution will
resume at the end of the loop's scope. Syntax is:
break;
For example, this processes steps until a manual step is hit (if no manual step is hit, all steps are processed):
for (var step: Step in steps) {
if (step.IsManual()) {
Print("Reached manual step!");
break;
}
step.Process();
}
continue
The continue statement immediately goes to the next loop of a while or
for. In a while, execution continues with the while expression. Syntax is:
continue;
For example, this prints all non-empty lines of a file, using continue to skip
empty lines:
var f: File = OpenFile(path);
while (!f.EOF()) {
var line: String = f.ReadLine();
if (line.IsEmpty()) {
continue;
}
Print(line);
}
Alternatives considered
References
- Proposal
#340:
while - Proposal
#353:
for - Proposal
#618:
varordering - Proposal #623: Require braces