R Program to implement if else statement

How to implement the if-else statement

Here we are explaining how to write an R program to implement the if-else statement. Here we are using a control statement if-else for checking. The if-else statement helps to check the condition based on the condition the expression is performed. The syntax of if-else statement is like,

if (condition1) { 
    expr1
    } else if (condition2) {
    expr2
    } else if  (condition3) {
    expr3
    } else {
    expr4
} 

 

How to implement the if-else statement in the R program

Below are the steps used in the R program to implement the if-else statement. In this R program, we first create a vector quantity and assign it with a value of 50. Check the value of quantity using if-else the statement. Based on the condition print the corresponding statement.

ALGORITHM

STEP 1: Assign variable quantity with vector value 50

STEP 2: Check the value of quantity using if statement as if (quantity > 40) 

STEP 3: Give the print statement inside if the condition

STEP 4: Give the print statement inside else the condition 

STEP 5: print the statement based on the true condition

R Source Code

                                          # Create vector quantity
quantity <-  50
# Set the is-else statement
if (quantity > 40) {
    print('Great you sold a lot!')
} else {
    print('Not enough for today')  
}
                                      

OUTPUT

## [1] "Great you sold a lot!"