Skip to content
Jean Dubois edited this page Jun 25, 2024 · 13 revisions

Loops

List

Nougaro Python C/C++
while while while
for for for
do ... then loop while (nothing) do ... while

Usage

  • 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!

Labels

You can label a loop using for:label, while:label or do:label. You can use labels with break and continue.

do ... then loop while behaviour

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.

break, continue

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 use break 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 and break work normally at the first execution of a do 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.

Returned value

  • By default, it returns a list of the values of <expr> after then. To get the last value, you can use the syntax list(-1) or list / -1.
  • If you use the break and return ... syntax, the loop return the value you specified.

Examples

  • 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 with example("reverse_string"))
Clone this wiki locally