From 09090bd29f1ca6fa5693ffff0d7059713848c0f4 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 20 Nov 2020 13:11:56 +0000 Subject: [PATCH] doc: explain labelled break and continue (#6890) --- doc/docs.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index 56ad15666b..05bb9ef3e2 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -947,6 +947,30 @@ stuck in an infinite loop. Here `i` doesn't need to be declared with `mut` since it's always going to be mutable by definition. +#### Labelled break & continue + +`break` and `continue` control the innermost `for` loop by default. +You can also use `break` and `continue` followed by a label name to refer to an outer `for` +loop: + +```v +outer: for i := 4;; i++ { + println(i) + for { + if i < 7 {continue outer} + else {break outer} + } +} +``` +The label must immediately precede the outer loop. +The above code prints: +``` +4 +5 +6 +7 +``` + ### Match ```v