-
Notifications
You must be signed in to change notification settings - Fork 1
Loops
Jean Dubois edited this page Jun 25, 2024
·
13 revisions
Nougaro | Python | C/C++ |
---|---|---|
while |
while |
while |
for |
for |
for |
do ... then loop while |
(nothing) | do ... while |
-
while <cond> then <statements> end
-
for <identifier> in <list|str> then <statements> end
-
for <identifier> = <start value> to <end value> (step <step>) <statements> end
-
do <statements> then loop while <condition>
while <cond> then <statement>
for <identifier> = <start value> to <end value> (step <step>) then <statement>
for <identifier> in <list|str> then <statement>
do <statement> then loop while <condition>
Note
In for
loops, the start value can be less than the end value. In that case, don’t forget to give a negative step!
You can label a loop using for:label
, while:label
or do:label
. You can use labels with break
and continue
.
The fist time the loop is executed, the do
body is executed even if the condition is false, then it executes like a normal while
loop.
Important
Prior to 0.20.0, break
and continue
did append None
to the return list. To re-enable this behaviour, you can enable appendNoneOnBreak
or appendNoneOnContinue
metas.
- The
break
statement completely stops the loop. If there is a returned list, the value is not added to this list. You can usebreak and return ...
to return a specific value. - The
continue
statement omit the code after it, and execute another time the loop like if the last time was ended. -
continue
andbreak
work normally at the first execution of ado
body. -
continue:label
will continue the loop that has this label. If you’re in an:inner
loop inside a:label
loop, the:inner
loop will break and the:label
loop will continue. -
break:label
will break the loop that has this label. If you’re in an:inner
loop inside a:label
loop, the:inner
AND the:label
loop will break.
- By default, it returns a list of the values of
<expr>
afterthen
. To get the last value, you can use the syntaxlist(-1)
orlist / -1
. - If you use the
break and return ...
syntax, the loop return the value you specified.
while foo == bar or variable <= foo then var foo = foo + 1
for foo = 10 to 20 then var bar = bar + foo + variable/3
for foo = -10 to 0 step 2 then var bar = bar + variable*2
for foo = 10 to -23 step -4 then var variable = bar + foo^3
for foo in [3, 2, "string", bar](1, 2, -1) then foo+bar
-
var list_ = [] for foo = 1 to 10 step 0.5 then if foo == 3 then continue if foo == 8.5 then break var list_ += foo end
In the case below, list_
is equal to [1, 1.5, 2.0, 2.5, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0]
.
-
var a = 0 var list_ = [] do append(list_, a) if a == 9 then; var a += 2; continue; end var a += 1 then loop while a != 20
In the case below, list_
is equal to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]
.
-
var list_ = [...] for i in list_ then if i%5 == 0 then break and return "The list contains a multiple of 5!" end
- example:
examples/reverse_string.noug
(execute withexample("reverse_string")
)