diff --git a/doc/userdoc.org b/doc/userdoc.org index 092dcc3d66759cf7959f04f4e3844edcf80f9ba2..fd14d7803047e26db1e982842b1a04deb57f6aaf 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