R Else If | R If elseif else Statement - r - learn r - r programming



  • In R programming, if statement can be followed by an optional else if...else statement, which is very suitable to test several conditions using single if...else if statement.
  • In general, Else If statement works like the if condition works if the condition is true, if condition is false the else condition works, certainly further on if the else condition is false once again the else condition will be executed.
 r if elseif

Read Also

Syntax

if( test_expression1) 
{
statement1
} 
else if( test_expression2) 
{
statement2
} 
else if( test_expression3) 
{
statement3
} 
else
statement4

Sample Code

x <- 0
if (x < 0) {
   print("This is even number")
} else if (x > 3) {
   print("This is odd number")
} else
   print("The value is Zero")

Read Also

Code Explanation

 r if code explannation
  1. Here we declare the variable x value as “5”.
  2. Here is the “if-elseif-else” statement where, the If condition executes the statements based on the true and false condition.Similarly, if the condition is false it executes the other elseif&if statements.

Output

 r code output
  1. Here in this output we get the “x” value as “5” and,where the elsecondition section will be satisfied so the output will be displayed as “The value is Zero”.


Related Searches to R If Else | R If elseif else Statement