Introduction to If Else Statement in R
If Else Statement in R language is a control statement that is used to check the logical condition and process the R statements inside the if block scope in case the condition returns a true boolean value and in the else block scope in case the condition expression returns a false boolean value.The if statements support multiple else statements as a part of complex logical scenarios in R language. There is a built-in function in R language known as ifelse(), which accepts condition expression, and R statements for if and else scenarios. The if-else statement in R is very useful and easy to use for statement selection and data processing while developing the program logic.
The Syntax of the if Statement
If ( cond ) {
Statement 1
Statement 2
Statement n
}
If cond is true, then all the statements inside the body are executed; else, nothing will be executed (no output). The cond is the condition that can be a numeric or logical vector. The all-numeric vector takes as TRUE except zero and takes the logical vector, a TRUE vector or FALSE vector.
The Flow Diagram of the if statement –
How does the if statement works?
Let us see some examples for if statement
Example #1
Code:
no <- 10
if (no > 0) {
print(" Number is positive ")
}
Output:
The above code checks whether the no is greater than zero; if true, then print ” Number is positive, “else nothing.
As in the above code, if the no is not greater than zero, nothing is performed, but if we need to perform or execute soothing, we can the else body.
The Syntax of the if-else statement
if ( cond ) {
statement 1
} else {
Statement 2
}
If cond is true, then all the statements inside the body are executed; else, if the cond is false, then all statements inside the else body are executed.
The important note here is the else should be in the same line as the if statement closing braces.
The Flow Diagram of the if-else statement
Example #2
The R code example to see how the if-else statement works with no = 10
Code:
no <- 10
if (no > 0) {
print(" Number is positive ")
} else {
print(" Number is Negative ")
}
Output:
Example #3
R code example to see how the if-else statement works with no = -10
Code:
no <- -10
if (no > 0) {
print(" Number is positive ")
} else {
print(" Number is Negative ")
}
Output:
Example #4
The above R code can be rewritten as a ternary operator
no <- -10
if (no > 0) print(" Number is positive ") else print(" Number is Negative ")
The if-else if statement
If not only one condition, if multiple conditions to be check and base on the specific condition the specific statement or statements to be executed, then the if-else if statement can be used.
The Syntax of the if-else if statement
if ( cond1 ) {
statement 1
} else if ( cond2 ) {
Statement 2
} else if (cond3 ) {
Statement 3
} else {
Statement 4
}
The if statement checks the condition, but if more than one or alternative conditions are to check, the alternative conditions can be checked with else if statement. Among all condition, only one condition gets true, so only one statement gets to execute.
Example #5
Code:
no <- 6
if (no < 0) {
print(" Number is negative ")
} else if (no > 10) {
print(" Number is positive and greater than 10 ")
} else
print(" Number is positive and less than 10 ")
Output:
Nested if statement
The if statement can have another if statement or even if-else inside it; it is called nested if statement and not only in if, the else part also can have nested if also nested if-else statement.
R code example where first it checks whether the no is positive or not and if the no is positive, it will check whether the no is even or not. For example, we take the no value as 6, so the no is positive.
Example #6
Code:
no <- 6
if( no>0 ){
print(" Number is positive ")
if( no %% 2==0 ){
print(" and Number is even ")
}
}
Output:
The output if no=5
So the output is displaying only the number is positive.
Next, we see some of the R program examples with the if-else statement; first, we write the program to print the bigger no.
Example #7
Code:
a <- as.integer(readline(prompt="Enter a value "))
b <- as.integer(readline(prompt="Enter b value "))
if( a>b ){
print(" a is greater than b ")
} else {
print(" b is greater than a ")
}
Output:
Example #8
Program to accept the marks and display the code as if the mark is in between 100 and 80 then grade A, else if mark between 80 and 50 then B, else grade C.
Code:
mark <- as.integer(readline(prompt="Enter mark "))
if( mark<=100 && mark>80 ){
print(" Your grade is A ")
} else if ( mark<=80 && mark>50 ) {
print(" Your grade is B ")
} else {
print(" Your grade is C")
}
Output:
Example #9
Program to check whether enter character is upper case letter or lower case letter.
Code:
ch <- readline(prompt=" Enter Character ")
if( ch>='A' && ch<='Z' ){
print(" Entered character is upper case ")
} else if ( ch>='a' && ch<='z' ) {
print(" Entered character is lower case ")
} else {
print(" You have not entered a Character ")
}
Output:
Conclusion
The if-else statement is the conditional construct in which the sequence of execution of statements decides based on the condition. In if statement, if cond is true, then all the statements inside the body executed, else nothing will be executed (no output). The cond is the condition that can be a numeric or logical vector. An if-else statement, if cond is true, then all the statements inside the body are executed, else if the cond is false, then all statements inside the else body are executed.
The if-else if statement used to check the multiple conditions. The if statement can have another if statement or even if – else inside it or and else can have inside another if-else statement, it is called nested if statement.
Recommended Articles
This is a guide to If Else Statement in R. Here, we discuss the R code examples to see how the if statement works with the programs and outputs. You may also look at the following article to learn more –