Conditional structures: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added a couple of SELECT-END comment labels. -- ~~~~)
m (clarify)
Line 2,729: Line 2,729:


===switch===
===switch===
<lang r>#Character input
<lang r># Character input
calories <- function(food) switch(food, apple=47, pizza=1500, stop("food not known"))
calories <- function(food) switch(food, apple=47, pizza=1500, stop("food not known"))
calories("apple") #47
calories("apple") # 47
calories("banana") #throws an error
calories("banana") # throws an error
#Numeric input
# Numeric input
alphabet <- function(number) switch(number, "a", "ab", "abc")
alphabet <- function(number) switch(number, "a", "ab", "abc")
alphabet(0) # null response
alphabet(1) # "a"
alphabet(2) # "ab"
alphabet(3) # "abc"
alphabet(3) # "abc"
alphabet(4) # NULL
alphabet(4) # null response
#Note that no 'otherwise' option is allowed when the input is numeric.</lang>
# Note that no 'otherwise' option is allowed when the input is numeric.</lang>


=={{header|Retro}}==
=={{header|Retro}}==