The If Else Statement allows us to choose between TRUE or FALSE, and when there are more than two options, we simply use Nested If Else statement. Say, What if we have 12 alternatives to choose?, if we use Nested If Else in this situation, programming logic will be difficult to understand. In R Programming Switch statement and Else if statement can handle these type of problems effectively.
We already discussed about the Else If Statement in our previous article. So, let us explore R Switch statement here.
The working functionality of the switch case in R programming is almost same as If Statement. As we said before, Switch statement in R may have n number of options. So, switch case compares the expression value with the values assigned in the position. If both the expression value and case value match then statements present in that position will execute. Let us see the syntax of R switch case for better understanding
R Switch syntax
The basic syntax of the Switch in R Programming language is as follows:
switch (Expression, "Option 1", "Option 2", "Option 3", ....., "Option N")
Bit complicated syntactical approach of Switch statement in r is:
switch (Expression, "Option 1" = Execute these statements when the expression result match Option 1, "Option 2" = Execute these statements when the expression result match Option 2, "Option 3" = When the expression result match Option 3,Execute these statements, .... .... "Option N" = When the expression result match Option N,Execute these statements, Default Statements )
- The expression value should be either integer or characters (We can write the expression as n/2 also but the result should be integer or convertible integers).
- R Switch statement allows us to add default statement. If the Expression value, or the Index_Position is not matching with any of the case statements then the default statements will be executed.
- If there is more than one match, the first matching statemnet will be returned.
R Switch case Flow Chart
The following screenshot will show you the flow chart behind this R Switch Case
- If Case = Option 1, STATEMENT 1 is executed.
- If Case = Option 2, STATEMENT 2 is executed.
- Case = Option 3, STATEMENT 3 is executed.
- If Option 1, Option 2, and Option 3 Fails then DEFAULT STATEMENT is executed.
R Switch Statement example
This program show you the basic functionality of the Switch in R programming language.
# R Switch Case Example switch(3, "Learn", "R Programming", "Tutorial", "Gateway" )
OUTPUT
Let us change the value and see
R Switch Case example
This program allows the user to enter any Arithmetic Operators to perform arithmetic operations using Switch statement in R programming language.
# R 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)) )
OUTPUT 1: Let us enter * operator and see
OUTPUT 2: Let us enter wrong operator $ to check the default value
ANALYSIS
First we declared two variables number1 and number2 and assigned some random values: 30 and 20
number1 <- 30 number2 <- 20
Following statement will allow the User to enter single character (it should be any Arithmetic operator) and we are assigning the user entered character to a variable called operator.
operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")
Next, we are using the R Switch statement with operator as an option. If user enters + as operator then following statement will be printed
print(paste("Addition of two numbers is: ", number1 + number2))
If user enters – as operator then following statement will be printed
print(paste("Subtraction of two numbers is: ", number1 - number2))
When user enters * as operator then below statement will be 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 )
Thank You for Visiting Our Blog