Conditional structures: Difference between revisions
Content added Content deleted
(→Conditional compilation: Fixed tags) |
(Added Kotlin) |
||
Line 2,389: | Line 2,389: | ||
end</lang> |
end</lang> |
||
Since jq's <tt>and</tt> and <tt>or</tt> are short-circuiting, they can also be used for branching. |
Since jq's <tt>and</tt> and <tt>or</tt> are short-circuiting, they can also be used for branching. |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
fun main(args: Array<String>) { |
|||
// conventional 'if/else if/else' statement |
|||
if (args.size == 0) println("No arguments were supplied") |
|||
else if (args.size == 1) println("One argument was supplied") |
|||
else println("${args.size} arguments were supplied") |
|||
print("Enter an integer : ") |
|||
val i = readLine()!!.toInt() |
|||
// 'when' statement (similar to 'switch' in C family languages) |
|||
when (i) { |
|||
0, 1 -> println("0 or 1") |
|||
in 2 .. 9 -> println("Between 2 and 9") |
|||
else -> println("Out of range") |
|||
} |
|||
// both of these can be used as expressions as well as statements |
|||
val s = if (i < 0) "negative" else "non-negative" |
|||
println("$i is $s") |
|||
val t = when { |
|||
i > 0 -> "positive" |
|||
i == 0 -> "zero" |
|||
else -> "negative" |
|||
} |
|||
println("$i is $t") |
|||
}</lang> |
|||
Sample input/output (program invoked without arguments): |
|||
{{out}} |
|||
<pre> |
|||
No arguments were supplied |
|||
Enter an integer : 3 |
|||
Between 2 and 9 |
|||
3 is non-negative |
|||
3 is positive |
|||
</pre> |
|||
=={{header|LabVIEW}}== |
=={{header|LabVIEW}}== |