Flow-control structures: Difference between revisions

jq
(jq)
Line 1,015:
* loop control with <code>'''break''' [label]</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/break]) and <code>'''continue''' [label]</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/continue])
* exceptions with <code>'''throw'''</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/throw]) and <code>'''try ... catch ... finally ...'''</code> ([http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/try...catch])
 
=={{header|jq}}==
 
jq 1.5 introduced `break` and `label` keywords for defining backtracking points. These are used to terminate a generator before completion. Here is a contrived example that illustrates the main points:
<pre>$ jq -n '1, (2 | label $foo | debug | 3 | break $foo | debug), 4'
1
["DEBUG:",2]
4</pre>
 
Here is an example from the standard library:
<lang jq># Emit at most one item from the stream generated by g:
def first(g): label $out | g | ., break $out;</lang>
 
=={{header|Kotlin}}==
 
Kotlin does not have a 'goto' statement but does have 'break' and 'continue' statements to jump out of or continue with the next iteration of a loop. The 'labelled' versions of these statements have already been described at [[Jump_anywhere#Kotlin]] and so only the basic versions are described in this task which jump out or continue with the nearest enclosing loop.
 
2,484

edits