Flow-control structures: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 976:
* 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|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.
 
Kotlin also has a 'throw' statement which throws (or rethrows) an exception.
 
Here are some examples:
<lang scala>// version 1.0.6 (packaged as Control_flow.jar)
 
fun main(args: Array<String>) {
for (i in 0 .. 2) {
for (j in 0 .. 2) {
if (i + j == 2) continue
if (i + j == 3) break
println(i + j)
}
}
println()
if (args.size > 0) throw IllegalArgumentException("No command line arguments should be supplied")
println("Goodbye!") // won't be executed
}</lang>
 
{{out}}
<pre>
c:\kotlin-compiler-1.0.6>java -jar control_flow.jar arg1 arg2
0
1
1
 
Exception in thread "main" java.lang.IllegalArgumentException: No command line arguments should be supplied
at Control_throwKt.main(control_throw.kt:12)
</pre>
 
=={{header|Lua}}==
Lua has the <code>break</code>-command to exit loops.
9,482

edits