Conditional Statements in R
Conditional Statements:
Conditional Statements allow you to specify which code should be executed depending on which conditions are matched.
Types of Conditional Statements in R:
In R programming, there are mainly three types of conditional statements:
-
i. if statement
ii. if-else statement
iii. switch statement
if statement:
The simplest form of decision-controlling statement for conditional execution is the ‘if’ statement. The ‘if’ statement produces a logical value and carries out the next statement only when that value becomes TRUE. In other words, an ‘if’ statement is having a Boolean expression followed by single or multiple statements.
if-else statement:
In this type of statement, the ‘if’ statement is usually followed by an optional ‘else’ statement that gets executes when the Boolean expression becomes false. This statement is used when you will be having multiple statements with multiple conditions to be executed.
switch statement:
A switch statement permits a variable to be tested in favour of equality against a list of case values. In the switch statement, for each case, the variable which is being switched is checked. This statement is generally used for multiple selections of condition-based statements.