Conditional structures: Difference between revisions

Added R
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
(Added R)
Line 5,532:
 
In general a dispatch table or class/object abstraction (using dynamic method over-rides) is considered preferable to chains of ''if ... elif ... elif ...'' in Python programming.
=={{header|R}}==
===if===
Like most languages, R has an if statement as well as if-then-else:
<lang r>x<-0
if(x==0) print("foo")
x<-1
if(x==0) print("foo")
if(x==0) print("foo") else print("bar")</lang>
{{out}}
<pre>> if(x==0) print("foo")
[1] "foo"
> if(x==0) print("foo")
> if(x==0) print("foo") else print("bar")
[1] "bar"</pre>
===switch===
R also has switch, but it's a function rather than a special form of any sort. In fact, R has two versions of switch: one for numbers and one for characters.
<lang r>x<-2
switch(x, print("Print if x==1"), print("Print if x==2"))</lang>
A notable part of the numeric version of switch is, rounding and coercion aside, the cases must correspond exactly to the number of arguments given minus one. For example, the second argument of the switch statement will only be matched if the first argument equals (or is coerced to) 1 and the third argument will only do so for 2. There is no way to supply default cases or start from a number other than 1.
<lang r>x<-3
switch(x, print("Print if x==1"), print("Print if x==2"))
x<-2.7
switch(x, print("Print if x==1"), print("Print if x==2 or if there is rounding to 2"))</lang>
The other switch, the one that works for characters, is much more sensible. Its rules are clearly laid out in documentation, but rely on R's mechanisms for names, which makes them a little bit complicated. See [https://cran.r-project.org/doc/manuals/r-release/R-lang.html#switch the language definition] for a reasonably simple example.
<lang r>x<-"match"
switch(x, mat = 0, match = 10, other = 100, 1000)
x<-"ma"
switch(x, mat = 0, match = 10, other = 100, 1000)
x<-"foo"
switch(x, mat = 0, match = 10, other = 100, 1000)</lang>
{{out}}
<pre>> switch(x, print("Print if x==1"), print("Print if x==2"))
[1] "Print if x==2"
> switch(x, print("Print if x==1"), print("Print if x==2"))
> switch(x, print("Print if x==1"), print("Print if x==2 or if there is rounding to 2"))
[1] "Print if x==2 or if there is rounding to 2"
> switch(x, mat = 0, match = 10, other = 100, 1000)
[1] 10
> switch(x, mat = 0, match = 10, other = 100, 1000)
[1] 1000
> switch(x, mat = 0, match = 10, other = 100, 1000)
[1] 1000</pre>
===ifelse===
R's final example is the ifelse function. Like switch, it is not a special form, so its inclusion here is debatable. In fact, the current version of the language definition does not even mention it. However, [https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Conditional-execution 'An Introduction to R'] gives a better
description than I could:
 
"''This has the form ifelse(condition, a, b) and returns a vector of the same length as condition, with elements a[i] if condition[i] is true, otherwise b[i] (where a and b are recycled as necessary).''"
 
Note also that it is not a ternary operator and its documentation warns against using it as such. In my experience, its most common use is in recoding data. For example:
<lang r>data<-c(1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0)
ifelse(data==1, "Yes", "No")</lang>
{{out}}
<pre>> ifelse(data==1, "Yes", "No")
[1] "Yes" "No" "Yes" "Yes" "Yes" "Yes" "Yes" "No" "No" "No" "No" "Yes" "Yes" "No" "Yes" "Yes" "Yes" "Yes" "Yes" "No" </pre>
=={{header|Racket}}==
 
331

edits