Conditional structures: Difference between revisions
Content added Content deleted
(Added Kotlin) |
(Added XLISP) |
||
Line 5,361: | Line 5,361: | ||
The je/jne jump instructions are again like jg/jle opposites of each other and again like je/jne rely on how the zero flag is set in the previous comparison. <br> |
The je/jne jump instructions are again like jg/jle opposites of each other and again like je/jne rely on how the zero flag is set in the previous comparison. <br> |
||
There are many different conditional jumps in assembly and many ways to set them, test, and, or to name a few. The ones covered are just some commonly used ones in order to show how assembly deals with conditional statements. |
There are many different conditional jumps in assembly and many ways to set them, test, and, or to name a few. The ones covered are just some commonly used ones in order to show how assembly deals with conditional statements. |
||
=={{header|XLISP}}== |
|||
===If=== |
|||
An <code>IF</code> expression has the form <code>(IF <condition> <then-clause> <opt-else-clause>)</code>, for example: |
|||
<lang lisp>(if (eq s "Rosetta Code") |
|||
"The well-known programming chrestomathy site" |
|||
"Some other website, maybe, I dunno" )</lang> |
|||
If the condition evaluates to anything except <code>NIL</code> or the empty list (which are equivalent), it is counted as true and the whole expression evaluates to the value of the <i>then</i> clause; otherwise it evaluates to the value of the optional <i>else</i> clause, if one is provided, or else to the empty list. |
|||
===Case=== |
|||
<code>CASE</code> expressions resemble the multi-way branching constructs found in most programming languages: an expression is evaluated, and the value of the whole expression is provided by the first clause that evaluates to a true value. Optionally, an <code>ELSE</code> expression can be provided, in case none of the clauses fits. |
|||
<lang lisp>(case s |
|||
("Rosetta Code" "Ah yes, the chrestomathy site") |
|||
("Stack Overflow" "Oh dear me, having problems are you?") |
|||
("Github" "Say no more") |
|||
(else "Sorry, never heard of it") )</lang> |
|||
===Cond=== |
|||
<code>COND</code> is a more general conditional than <code>IF</code> or <code>CASE</code>: it resembles a <code>CASE</code> statement, but with the option of using a different conditional expression in each clause. A default value can be provided using <code>ELSE</code>, as with <code>CASE</code>, or any expression that is guaranteed to return a value other than <code>NIL</code> or the empty list. |
|||
<lang lisp>(cond |
|||
((eq s "Rosetta Code") "Chrestomathy site") |
|||
((> n 37) "Some other appropriate value, presumably") |
|||
(t "If you're seeing me, s wasn't equal to Rosetta Code and n must have been 37 or below") )</lang> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |