Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
d450c057
Commit
d450c057
authored
May 30, 2022
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Describe for loops
parent
ceee5ebb
No related branches found
No related tags found
1 merge request
!145
git subrepo clone git@gitlab.com:OlMon/org-themes.git packages/org-themes
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
doc/userdoc.org
+59
-1
59 additions, 1 deletion
doc/userdoc.org
with
59 additions
and
1 deletion
doc/userdoc.org
+
59
−
1
View file @
d450c057
...
...
@@ -1878,7 +1878,65 @@ We give a final illustration
#+END_SRC
#+results: if-block
*** TODO for loops
*** ~for~ loops
~pugs~ allow to write ~for~ loops. It follows the ~C++~ syntax
#+BEGIN_SRC pugs :exports code
for (declarationinstruction ; condition ; postinstruction) statement
#+END_SRC
Following ~C++~, ~declarationinstruction~, ~condition~ and ~postinstruction~
are optional. The ~condition~ argument if present *must* be a boolean
value (type ~B~), if it is absent, the default value ~true~ is used.
- The ~declarationinstruction~ is execute only *once* /before/ the beginning
of the loop. The lifetime of the variable declared here is defined
by the ~for~ instruction itself.
- The ~condition~ is evaluate /before/ each loop.
- The ~postinstruction~ is executed /after/ each loop.
- The ~statement~ is either a single instruction or a block of
instructions. The ~statement~ is executed if the ~condition~ has the
value ~true~.
For instance, one can write
#+NAME: for-block
#+BEGIN_SRC pugs :exports both :results output
let n:N, n = 10;
let sum:N, sum = 0;
for (let i:N, i=1; i<=n; ++i) {
sum += i;
}
cout << "sum = " << sum << "\n";
#+END_SRC
which gives as expected
#+results: for-block
The lifetime of the declared variable (in the ~declarationinstruction~
statement) is illustrate by the following example
#+NAME: for-scope-error
#+BEGIN_SRC pugs-error :exports both :results output
for (let i:N, i=0; i<2; ++i) {
cout << "i = " << i << "\n";
}
cout << "i = " << i << "\n";
#+END_SRC
Running this example produces the following error
#+results: for-scope-error
To fix the previous code, one can write
#+NAME: for-no-decl
#+BEGIN_SRC pugs :exports both :results output
let i:N, i=0;
for (; i<2; ++i) {
cout << "i = " << i << "\n";
}
cout << "i = " << i << "\n";
#+END_SRC
One gets
#+results: for-no-decl
*** TODO do while loops
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment