Conditional structures: Difference between revisions

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
Line 5,796: Line 5,796:
===switch===
===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.
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
<lang rsplus>x<-2
switch(x, print("Print if x==1"), print("Print if x==2"))</lang>
switch(x, print("Print if x==1"), print("Print if x==2"))</lang>
A notable part of the numeric version of switch is that, 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.
A notable part of the numeric version of switch is that, 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
<lang rsplus>x<-3
switch(x, print("Print if x==1"), print("Print if x==2"))
switch(x, print("Print if x==1"), print("Print if x==2"))
x<-2.7
x<-2.7
switch(x, print("Print if x==1"), print("Print if x==2 or if there is rounding to 2"))</lang>
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.
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"
<lang rsplus>x<-"match"
switch(x, mat = 0, match = 10, other = 100, 1000)
switch(x, mat = 0, match = 10, other = 100, 1000)
x<-"ma"
x<-"ma"
Line 5,835: Line 5,835:
<pre>> ifelse(data==1, "Yes", "No")
<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>
[1] "Yes" "No" "Yes" "Yes" "Yes" "Yes" "Yes" "No" "No" "No" "No" "Yes" "Yes" "No" "Yes" "Yes" "Yes" "Yes" "Yes" "No" </pre>

=={{header|Racket}}==
=={{header|Racket}}==