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
ceee5ebb
Commit
ceee5ebb
authored
May 30, 2022
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Add if...else statement description
parent
79651a8d
Branches
Branches containing commit
Tags
Tags containing commit
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
+92
-8
92 additions, 8 deletions
doc/userdoc.org
with
92 additions
and
8 deletions
doc/userdoc.org
+
92
−
8
View file @
ceee5ebb
...
@@ -1742,7 +1742,7 @@ section [[functions]], functions can return compound values, thus compound
...
@@ -1742,7 +1742,7 @@ section [[functions]], functions can return compound values, thus compound
affectations (or definitions) are needed to get returned values in
affectations (or definitions) are needed to get returned values in
that case.
that case.
***
TODO
Tuple types<<tuples>>
*** Tuple types<<tuples>>
The last special type construction is the ability to deal with tuples
The last special type construction is the ability to deal with tuples
in the ~pugs~ language. The tuples we are describing here are lists of
in the ~pugs~ language. The tuples we are describing here are lists of
...
@@ -1790,17 +1790,101 @@ instructions.
...
@@ -1790,17 +1790,101 @@ instructions.
underlying methods. A classical example is to provide a set of
underlying methods. A classical example is to provide a set of
boundary conditions to a method.
boundary conditions to a method.
** TODO Statements
** Statements
The ~pugs~ language supports classical statements to control the data
flow. For simplicity, these statements syntax follows their ~C++~
counterpart. The only statement that is not implemented in ~pugs~ is the
~switch...case~. This may change but in one hand, up to it was never
necessary (we did not use chained ~if...else~ statements), and on the
other hand, keeping the language as simple as possible remains the
policy in ~pugs~ development.
*** ~if...else~ statement
The simplest statement is the conditional statement ~if...else~. If a
condition is satisfied, the ~truestatement~ (a single instruction or
block of instructions) is executed. In the other case, if the /optional/
~else~ keyword is used, the ~falsestatement~ (a single instruction or
block of instructions) is executed.
#+BEGIN_SRC pugs :exports code
if (condition) truestatement
if (condition) truestatement else falsestatement
#+END_SRC
The condition itself *must be* a boolean value (of type ~B~).
#+NAME: if-instruction
#+BEGIN_SRC pugs :exports both :results output
if (2>1) cout << "yes: 2 > 1\n";
if (not (2>1)) cout << "hmm... 2 <= 1 ?\n";
#+END_SRC
generates the following expected output. The second output is
hopefully not executed.
#+results: if-instruction
It is probably better to use the ~else~ keyword to obtain the same
result
#+NAME: if-else-instruction
#+BEGIN_SRC pugs :exports both :results output
if (2<=1)
cout << "hmm... 2 <= 1 ?\n";
else
cout << "ok: 2 > 1\n";
#+END_SRC
#+results: if-else-instruction
However, it is recommended even in the case of single instructions in
the conditional statements to use instruction blocks:
#+NAME: if-else-block-of-one
#+BEGIN_SRC pugs :exports both :results output
import math;
let sin100_positive:B;
*** if/else
if (sin(100) > 0){
sin100_positive = true;
} else {
sin100_positive = false;
}
cout << "sin(100)>0: " << sin100_positive << "\n";
#+END_SRC
#+results: if-else-block-of-one
Obviously in this simple case, it would be better to write
#+BEGIN_SRC pugs :exports both :results output
import math;
let sin100_positive:B, sin100_positive = (sin(100) > 0);
// parenthesis are useless but ease reading
cout << "sin(100)>0 ? " << sin100_positive << "\n";
#+END_SRC
but this is not the purpose here.
We give a final illustration
#+NAME: if-block
#+BEGIN_SRC pugs :exports both :results output
let m:R, m = 2.5;
let M:R, M = 2;
let has_swapped:B, has_swapped = false;
if (m>M){
let tmp:R, tmp = m;
m = M;
M = tmp;
has_swapped = true;
}
cout << "min = " << m << " max = " << M << " has_swapped = " << has_swapped << "\n";
#+END_SRC
#+results: if-block
*** for loops
***
TODO
for loops
*** do while loops
***
TODO
do while loops
*** while loops
***
TODO
while loops
*** break/continue
***
TODO
break/continue
** TODO Functions<<functions>>
** TODO Functions<<functions>>
...
...
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