R Switch Statement

The If Else Statement allows us to choose between TRUE or FALSE, and when there are more than two options, we use the Nested If Else statement. Say, what if we have 12 alternatives to choose?, if we use Nested if-else in this situation, the programming logic will be difficult to understand. In R Programming, the Switch statement and Else if can handle these types of problems effectively.

The working functionality of the switch case in this programming is almost the same as the If Statement. As we said before, the Switch statement in R may have n number of options. So, the switch case compares the expression value with the values assigned in the position. If both the expression value and case value match, then the code present in that position will execute. Let us see the syntax of the switch case for a better understanding.

R Switch Statement syntax

The basic syntax of the Switch in this Programming language is

switch (Expression, "Option 1", "Option 2", "Option 3", ....., "Option N")

Bit complicated syntactical approach of the Switch case statement in r is:

switch (Expression,
        "Option 1" = Execute when the expression result match Option 1,
        "Option 2" = Execute when the expression result match Option 2,
        "Option 3" = When the expression result match Option 3, Execute these,
        ....
        ....
        "Option N" = When the expression result match Option N, Execute these lines,
        Default Statements
)
  • The expression value should be either an integer or characters (We can write the expression as n/2 also, but the result should be an integer or convertible integer).
  • A Switch statement allows us to add a default statement. If the Expression value or the Index_Position does not match with any of the cases, then the default statements will be executed.
  • If there is more than one match, the first matching statement will be returned.

We already discussed the Else If in our previous R Programming article. So, let us explore the Switch statement here. I suggest you refer to the If Else, Nested If Else, and If clause articles.

R Switch case Statement Flow Chart

The following screenshot will show you the flow chart behind this Switch Case

  • If Case = Option 1, STATEMENT1 is executed.
  • If Case = Option 2, STATEMENT2 is executed.
  • Case = Option 3, STATEMENT3 is executed.
  • If Option 1, Option 2, and Option 3 Fail, then the DEFAULT STATEMENT is executed.

Switch Statement Example

This program showsyou the basic functionality of the Switch Case in this programming language.

switch(3, 
"Learn",
"R Programming",
"Tutorial",
"Gateway"
)
R Switch Statement 1

Let us change the value and see the result. Instead of 3, this example uses 2 as the first argument.

switch(2, 
"Learn",
"R Programming",
"Tutorial",
"Gateway"
)
"R Programming"

R Switch Case example

This program allows the user to enter any Arithmetic Operators to perform arithmetic operations using the Switch statement in this programming language.

# Switch Statement Example

number1 <- 30
number2 <- 20
operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")

switch(operator,
"+" = print(paste("Addition of two numbers is: ", number1 + number2)),
"-" = print(paste("Subtraction of two numbers is: ", number1 - number2)),
"*" = print(paste("Multiplication of two numbers is: ", number1 * number2)),
"^" = print(paste("Exponent of two numbers is: ", number1 ^ number2)),
"/" = print(paste("Division of two numbers is: ", number1 / number2)),
"%/%" = print(paste("Integer Division of two numbers is: ", number1 %/% number2)),
"%%" = print(paste("Division of two numbers is: ", number1 %% number2))
print("default statement")
)

OUTPUT 1: Let us enter * operator and see

R Switch Statement 2

OUTPUT 2: Let us enter the wrong operator $ to check the default value.

"default statement"

First, we declared two variables, number1 and number2, and assigned some random values: 30 and 20

number1 <- 30
number2 <- 20

The following statement will allow the User to enter a single character (it should be any Arithmetic operator). And we are assigning the user entered character to a variable called an operator.

operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")

Next, we are using the R Switch statement with the operator as an option. If the user enters + as an operator, then the following code is printed.

print(paste("Addition of two numbers is: ", number1 + number2))

If the user enters – as an operator, then the following line will print.

print(paste("Subtraction of two numbers is: ", number1 - number2))

When the user enters * as an operator, then the below message is printed.

print(paste("Multiplication of two numbers is: ", number1 * number2)

If the user-entered operator (character) is not in any of the above, then you can assign some default statement like:

switch(operator,
 "+" = print(paste("Addition of two numbers is: ", number1 + number2)),
 "-" = print(paste("Subtraction of two numbers is: ", number1 - number2)),
 "*" = print(paste("Multiplication of two numbers is: ", number1 * number2)),
 "^" = print(paste("Exponent of two numbers is: ", number1 ^ number2)),
 "/" = print(paste("Division of two numbers is: ", number1 / number2)),
 "%/%" = print(paste("Integer Division of two numbers is: ", number1 %/% number2)),
 "%%" = print(paste("Division of two numbers is: ", number1 %% number2)),
 print("default") # Default Statement
)