# R – Switch , Break, Next Statement

### R Switch syntax

The basic syntax of the Switch in R Programming language is

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

Bit complicated syntactical approach of the 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 an integer or convertible integers).
• An R Switch statement allows us to add a 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 statement 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 Case example

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

```# R Switch Statement Example

number1 <- 30
number2 <- 20

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 the 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```

The following statement will allow the User to enter the 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 statement printed.

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

If the user enters  as an operator, then the following statement printed.

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

When the user enters * as an operator, then the below statement 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
)```

### R Break Statement

The R Break statement is very useful to exit from any loop such as For Loop, While Loop, and Repeat Loop. While executing these loops, if R finds the break statement inside them, it will stop executing the statements and immediately exit from the loop.

For example, we have 15 statements inside the loop, and we want to exit from the loop when a certain condition is True; otherwise, it has to execute all of them. In this situation, we can use If Statement to check for the expression and place the Break statement inside the If block. If the condition is True, then it will execute the break statement, and the break will exit the controller from the loop completely. Otherwise, it will execute all the statements.

#### R Break statement Syntax

The syntax of the Break Statement in R Programming language is

`break`

In this article, We would like to share 2 examples to display the working functionality of the Break statement in both While Loop and For Loop. Please refer R Repeat Loop article to understand the Repeat loop example

#### R For Loop Break Statement

In this R program, we use the break statement inside the for loop to exit from the loop iteration.

```# R Break Statement Example

number <- 1:10

for (val in number)  {
if (val == 7)  {
print(paste("Coming out from for loop Where i =  ", val))
break
}
print(paste("Values are :  ", val))
}```

OUTPUT

ANALYSIS

Within the For loop, we placed If Statement to test whether i is equal to 7.

• If the expression is false, then it will skip the Break statement and prints that number as output (In Our case 1, 2, 3, 4, 5, 6).
• If the expression is True, then the Break statement executed, and the iteration will stop at that number without printing the statement. print(paste(“Values are : “, val)).

#### R While Loop Break Statement

In this program, we use the R break statement inside the While loop to exit from the loop iteration.

```# R Break Statement Example

number <- 10

while (number > 0)  {
if (number == 3)  {
print(paste("Coming out from While loop Where number =  ", number))
break
}
print(paste("Values are :  ", number))
number = number - 1
}```

OUTPUT

ANALYSIS

First, We initialized the value of the number to 10 at the beginning of the code. Within the While loop, we check for the condition whether the number is greater than 0 or not.

`while (number > 0)  {`

Inside the While loop, we placed the If Statement to test whether i is equal to 3.

• If the condition is false, then it will skip the Break statement and prints that number as output (In Our case 10, 9, 8, 7, 6, 5, 4).
• If this condition is True, then the R Break statement executed, and the iteration will stop at that number without printing the print(paste(“Values are : “, number)) statement.

### R Next Statement

The R Next statement is one of the most useful statements that control the flow of R loops. We generally use this R Next statement inside the For Loop and While Loop. While executing these loops, if the compiler finds the R Next statement inside them, it will stop the current loop iteration and starts the new iteration from the beginning.

For example, If we have 10 statements inside the loop, and we want to skip executing the second 5 statements (statement 6 — statement 10) when a certain condition is True. Otherwise, it has to execute all the 10 statements inside the loop. In this situation, we place the If Statement with a certain condition and within that place the R Next statement. If the condition is True, it will stop executing statements 6 to 10. Otherwise, it will execute statements 1 to 10.

#### R Next Statement Syntax

The syntax of the Next Statement in R Programming language is

`Next`

In this article, we would like to share two examples to display the working functionality of the R Next statement in both While Loop and For Loop.

#### R Next Statement in For Loop

In this program, we show how to use R Next Statement inside the For Loop with example. This program will display the Even and Odd numbers inside the given range.

```# R Next Statement Example

number <- 1:20

for (val in number) {
if (val %% 2 != 0)  {
print(paste("ODD Number =  ", val, "(Skipped by Next Statement)"))
next
}
print(paste("EVEN Number =  ", val))
}```

OUTPUT

ANALYSIS

Inside the for loop, we placed If Statement to test whether (val %% 2 != 0).

• If this expression result is True, the R Next statement is executed. And the iteration will stop at that number without printing the other statement: print(paste(“EVEN Number = “, val)).
• If the expression result is false, then it will skip the Next statement and print that number as output (In Our case Even Number).

#### Next Statement in R While Loop Example

In this program, we show how to use R Next Statement inside the While Loop with example. This program allows the user to enter any integer values. Then it will display all the values from 0 to a given number except 4 and 7.

```# R Next Statement Example

number <- 0

while (number <= 10)  {
if (number == 4 || number == 7)  {
print(paste("Skipped by the Next Statement =  ", number))
number = number + 1
next
}
print(paste("Values are :  ", number))
number = number + 1
}```

OUTPUT

ANALYSIS

Inside the While loop, we placed If Statement to test whether i is equal to 4 or 7.

• If this condition is True, then the R Next statement executed, and the iteration will stop at that number without printing the other statement: print(paste(“Values are : “, number)). For better understanding, we placed print(paste(“Skipped by the Next Statement = “, number)) inside the If condition. So, whenever the iteration break, that value be printed from this statement.
• If the condition is false, then it will skip the next statement and print that number as output (In Our case, 0,1,2,3,5,6,8,9,10).