What are the control statements in the R programming language
What are the control statements in the R programming language?
Control Statements
Control statements use for decision-making. In the control statement, the programmer mentions the conditions that test by the programs. If the specified condition is true, it executes the set of statements and vice-versa.
Types of control statements in R language:
1) If statement
2) If else statement
3) Else If statement
4) Switch statement
If Statement
if statement is a control statement. It uses to check the condition based on the programming logic. It checks the if condition for true or false cases. If the if condition is satisfied, it will execute the statement written inside the block. Similarly, if the conditions turn out to be false, the control will come out of the if block.
Syntax of the if statement:
if (condition) {
statement_1
statement_2
statement_n # The statements will execute if the condition is satisfied.
}
If Else statement
In the R language, the if-else statement is a control statement used to check the logic condition. It provides the boolean values that are true or false. If the if condition is determined as true, it will execute the statements written inside the if block. However, if the If condition is not satisfied, it will return the statements written inside the else block.
Syntax of the If Else statement
if (condition) {
statement_1 # The statement will execute if the condition is satisfied.
}
else {
statement_2 # The statement will execute if the condition turns out to be not satisfied.
}
Else If statement
It continuously validates certain conditions. If any of the if condition is satisfied, it will return statements written inside the block. If none of the if condition is true, it will execute the statements written inside the else block.
Syntax of the else If statement:
if (condition){
# expression
} else if (condition){
# expression
} else if (condition){
# expression
} else {
statement # The statement will execute if none of the if conditions turns out to be true.
}
Switch Statement
Switch Statement is a control statement in R language. It has a set of values. The value that will satisfy all the conditions of the problem statement will execute. If none of the values in the switch statement satisfied the condition in the given statement, then the statement written under the default value executes.
Syntax of the switch statement:
switch(expression, Value 1, Value 2, Value 3.... Value n)