diff --git a/TODO b/TODO index 6e9d5cb..9c425e6 100644 --- a/TODO +++ b/TODO @@ -8,6 +8,7 @@ - stdlib moves line of file, which makes error reporting impossible # Features +- number += 1 (also update docs!) - Expect return statement if specified in function - Type inference - Multi-file support diff --git a/docs/concepts/control-flow.md b/docs/concepts/control-flow.md index 665bf35..971a9fb 100644 --- a/docs/concepts/control-flow.md +++ b/docs/concepts/control-flow.md @@ -77,4 +77,26 @@ When this program executes, it checks each `if` expression in turn and executes ## Loops -TODO +It's often useful to execute a block of code more than once. For this task, Sabre provides different kind of _loops_. A loop runs through the code inside the its body to the end and then starts immediately back at the beginning. + +Sabre has two types of loops: `while` and `for`. Let's go through each of them. + +### Conditional Loops with `while` + +It’s often useful for a program to evaluate a condition within a loop. While the condition is true, the loop runs. When the condition ceases to be true, the program calls `break`, stopping the loop. + +The example below loops three times, counting down each time, and then, after the loop, it prints another message and exits. + +``` +fn main() { + let number = 3 + + while number != 0 { + println(number) + + number = number - 1 + } + + println("LIFTOFF!!!") +} +``` diff --git a/examples/sandbox.sb b/examples/sandbox.sb new file mode 100644 index 0000000..f771b8b --- /dev/null +++ b/examples/sandbox.sb @@ -0,0 +1,11 @@ +fn main() { + let number = 3 + + while number != 0 { + println(number) + + number = number - 1 + } + + println("LIFTOFF!!!") +} \ No newline at end of file