From e7863e8964becbbcf8ada621163156cbb37e8d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Mon, 30 May 2022 16:57:07 +0200 Subject: [PATCH] Add do...while documentation --- doc/userdoc.org | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/doc/userdoc.org b/doc/userdoc.org index 092dcc3d6..fd14d7803 100644 --- a/doc/userdoc.org +++ b/doc/userdoc.org @@ -1880,7 +1880,7 @@ We give a final illustration *** ~for~ loops -~pugs~ allow to write ~for~ loops. It follows the ~C++~ syntax +~pugs~ allows to write ~for~ loops. It follows the ~C++~ syntax #+BEGIN_SRC pugs :exports code for (declarationinstruction ; condition ; postinstruction) statement #+END_SRC @@ -1937,8 +1937,43 @@ To fix the previous code, one can write One gets #+results: for-no-decl +*** ~do...while~ loops -*** TODO do while loops +The second kind of loops that can be written is the ~do...while~ +construction which executes /at least/ one time the enclosed ~statement~. +#+BEGIN_SRC pugs :exports code + do statement while (condition); +#+END_SRC + +The ~statement~ is either a single instruction or a block of +instructions. The ~condition~ is an expression of boolean value (type +~B~). + +#+NAME: do-while-instr +#+BEGIN_SRC pugs :exports both :results output + let sum:N, sum = 0; + let i:N, i = 0; + do + sum += i++; + while (i<=10); + cout << "sum = " << sum << "\n"; +#+END_SRC +This code produces +#+results: do-while-instr + +However, to ease the reading, it is probably better to write +#+NAME: do-while-block +#+BEGIN_SRC pugs :exports both :results output + let sum:N, sum = 0; + let i:N, i = 0; + do { + sum += i; + ++i; + } while (i<=10); + cout << "sum = " << sum << "\n"; +#+END_SRC +It gives also +#+results: do-while-block *** TODO while loops -- GitLab