![]() |
VOOZH | about |
Switch case
statements are a substitute for long if statements that compare a variable to several integral values. Switch case in R is a multiway branch statement. It allows a variable to be tested for equality against a list of values. Switch statement follows the approach of mapping and searching over a list of values. If there is more than one match for a specific value, then the switch statement will return the first match found of the value matched with the expression.
Syntax:
switch(expression, case1, case2, case3....)Here, the expression is matched with the list of values and the corresponding value is returned.
Important Points about Switch Case Statements:
Flowchart:
Example 1:
Output:
[1] "Geeks4"Example 2:
Output:
multiplication = 42NULL