Conditional structures: Difference between revisions

Adds slope example
m (syntax highlighting fixup automation)
(Adds slope example)
Line 6,862:
 
<syntaxhighlight lang="slate">[p isAtEnd] whileFalse: [p next evaluate]].</syntaxhighlight>
 
=={{header|Slope}}==
 
The following examples are highly derived, but should give the basics. All of the forms below (if, case, cond) return the result of evaluating their associated expression/consequent. The examples below tend toward side effects, so mostly return the empty list.
 
===if===
 
Syntax:
<syntaxhighlight lang="text">(if [test] [consequent] [[alternate]])</syntaxhighlight>
 
Example:
 
<syntaxhighlight lang="slope">(define my-file (file-open-read "my-file.txt"))
(if my-file (write (read-all my-file)) (! "Could not open file"))</syntaxhighlight>
 
===case===
 
Syntax:
<syntaxhighlight lang="text">(case [value] (match-val expression)...)</syntaxhighlight>
 
Example:
<syntaxhighlight lang="slope">
(case my-enum
(string (do-string my-enum))
(bool (do-bool my-enum))
(proc (my-enum))
(number (do-number my-enum))
(else (! "No match found in case")))</syntaxhighlight>
 
===cond===
 
Syntax:
<syntaxhighlight lang="text">(cond ([test] [expression])...)</syntaxhighlight>
 
Example:
<syntaxhighlight lang="slope">(define my-num 123)
(cond
((positive? my-num) 'positive)
((negative? my-num) 'negative)
(else 'zero))</syntaxhighlight>
 
 
 
 
 
=={{header|Smalltalk}}==
37

edits