# R If Else Statement

In the real programming world, the R If Statement is the primary decision-making statement. The R If Else Statement is an extension of the If Statement. The If clause tests the condition first and executes the statements depending upon the result. If the test condition is true, only the code within the if block executes.

In R programming language, If Else statements play a crucial role in making decisions and controlling a program’s flow. Let us see how to use this R IF Else Statement in real-time with an example.

This article shows how to write an If else statement in R programming, its syntax, and use different types of conditional operators. We start with a simple example and delve into more complex conditions. Furthermore, we cover the R nested if else statements and handle multiple conditions.

In the real programming world, the R If Statement is the primary decision-making statement. The If clause tests the condition first and executes the statements depending upon the result. If the test condition is true, only the code within the if block executes.

## R If Statement Syntax

The syntax of the If statement in R Programming language has a simple structure.

```if (Boolean_Expression)  {

Statement 1;
Statement 2;
………….
………….
Statement n;
}```

From the above R code snippet, If the Boolean_Expression / test condition inside the If statement is true, then the Statement1, Statement2,……., StatementN executed. Otherwise, all those statements are skipped.

### R If Statement Flow Chart

The below picture shows the flow chart behind the If Statement.

• If the test condition is true, STATEMENT1 executes, followed by STATEMENTN.
• If the condition is False, STATEMENTN executes because it is placed outside of the if condition block and it has nothing to do with the condition result. Let us see one example to understand better.

### R If Statement Example

This program allows the user to enter any positive integer and checks whether the user-specified number is Positive or Not.

```number <- as.integer(readline(prompt="Please Enter any integer Value: "))

if (number > 1) {
print("You have entered POSITIVE Number")
}```

NOTE: If statement in this Programming does not require the curly brackets to hold a single line. But, It is always good practice to use curly brackets.

As you can see that we entered 25 as a number. This if statement program checks whether 25 is greater than one or not. We all know that it is True; that’s why the program is printing the text inside the print().

Let us change the number value to check what happens if the Boolean expression fails. (number < 1).

It prints nothing because -12 is less than 1, and we don’t have anything to print after the if statement block. I hope you are confused. Let us see one more example.

### If statement with multiple statements Example

This program for the R if statement enables the user to enter any positive integer and check whether the number is positive or Not.

```number <- as.integer(readline(prompt="Please Enter any integer Value: "))

if (number > 1) {
print("You have entered POSITIVE Number")
}
print("This is not the Message coming from inside")
print("This Message is from Outside")```

You can observe the output below. It printed all the print lines because 14 is greater than 1.

Let’s try the negative values to fail the condition in the R If statement deliberately.

Here, the Boolean expression inside the If statement failed (number > 1). That’s why it prints nothing from the If block. However, it printed the code that is outside the If block.

## R If Else Statement

The If statement executes the code only when the given condition is true. If the condition is false, it will not execute any code block. However, implementing something in real-world programming would be nice when the condition fails. To do so, we can use this R If else statement. Here, When the condition fails, the Else block executes the code.

### R If Else Statement Syntax

The syntax of the If Else statement in R Programming language follows a specific structure. It starts with the if keyword and a set of parentheses to hold the condition logic, followed by the curly braces.

The condition inside the R if-else statement can be any expression. You can use a single or multiple comparison or logical operators. If the test condition / boolean expression evaluates to true, then the True code will execute. If the expression evaluates as false, then a False code will run.

```if (Boolean_Expression)  {
# The Boolean_Expression result is TRUE, then it will execute True code.
} else  {
# The Boolean_Expression result is FALSE,then it will execute a False code.
}```

### R If Else Statement Flow Chart

The following picture shows you the flow chart behind the If Else statement.

In the above R If Else statement flow chart,

• If the test condition is true, STATEMENT1 is executed, followed by STATEMENTN.
• If the condition is False, then STATEMENT2 is executed, followed by STATEMENTN. Here, STATEMENTN is executed irrespective of test results. Because it is placed outside the If Else condition block, it has nothing to do with the condition result.

### R If Else Statement Example

This program allows the user to enter their age. Then, it checks whether they are eligible to vote using the if else statement in this Programming.

In this R if else statement program, we will place 4 different print messages. If the condition is true, we will print two separate statements. If the condition is false, we will print another two statements. Please refer to the If condition article.

```my.age <- as.integer(readline(prompt="Please Enter your Age: "))

if (my.age > 18) {
print("You are eligible to Vote.") # 1
print("Don't forget to carry Your Voter ID's to Polling booth.") # 2
} else {
print("You are NOT eligible to Vote.") # 3
print("We are Sorry") # 4
}
print("This Message is from Outside") # 5```

The user enters their age. If the age is greater than or equal to 18, #1 and #2 are printed. If the age is less than 18, then 3 and 4 are printed as output. Outside the R If Else statement block, we placed one print function #5. This message (#5) will execute irrespective of the expression result.

OUTPUT 1: Let us enter the age of 32. expression result is TRUE.

Let us enter age = 17 to fail the condition. So, it is FALSE.

### Using logical and Comparison operators in the R If Else Statement

The above example is a simple example using comparison operators. However, combining the comparison and logical operators creates a complex condition and performs powerful operations.

In this example, the if condition checks whether the age is greater than 18 and the income exceeds 700,000. If true, you are eligible to pay taxes; otherwise, you can enjoy the tax-free year.

```age <- 27
income <- 900000

if (age > 18 && income > 700000) {
} else {
print("Enjoy the Tax Free Year!")
}```
``[1] "Please Pay your Personal Tax."``

You can also experiment the if else statement with “|| and !” Logical operators and “<, <=, >=, ==, and !=“ comparison operators.

## Nested If Else in R

Place one If statement inside another, called Nested If Else in R Programming. The If Else statement allows us to print different statements depending upon the expression result (TRUE or FALSE). However, sometimes we have to check further when the condition is TRUE. In these situations, we can use this Nested If Else concept in R, but please be careful while using it.

When dealing with multiple conditions to make a complex decision, nested structures play a crucial role. Before the example, let us see the syntax behind the R Nested If Else statement.

### Nested If Else in R Syntax

The basic syntax of the Nested If Else Statement in R Programming language is as follows:

```if (Boolean_Expression 1)  {
#Boolean_Expression 1 result is TRUE then, it will check for Boolean_Expression 2
if (Boolean_Expression 2)  {
#Boolean_Expression 2 result is TRUE, then these statements will be executed
Boolean_Expression 2 True statements
} else {
#Boolean_Expression 2 result is FALSE then, these statements will be executed
Boolean_Expression 2 False statements
} else  {
#If the Boolean_Expression 1 result is FALSE, these statements will be executed
Boolean_Expression 1 False statements
}```

### Nested If Else in R Flow Chart

The following picture shows you the flow chart of the Nested If statement in R Programming.

• If Test Condition 1 is FALSE, then STATEMENT 3 will run.
• If Test Condition 1 is TRUE, it will check for Test Condition 2. And if it is TRUE, then STATEMENT 1 will run. Otherwise, STATEMENT 2 will run.

### Nested If Else in R Programming Example

This R Nested If Else program allows the user to enter their age, and then we will store it in the variable my.age. If the user-specified age is less than 18, we will print two statements. If the condition fails, we check one more condition (R Nested If Else); if it succeeds, we print something. And If the nested condition fails, we print some other thing.

```# Nested IF Else in R Programming Example

if (my.age < 18) {
print("You are Not a Major.")
print("You are Not Eligible to Work")
} else {
if (my.age >= 18 && my.age <= 60 )  {
print("You are Eligible to Work")
print("Please fill the Application Form and Email to us")
} else {
print("As per the Government Rules, You are too Old to Work")
}
}
print("This Message is from Outside the Nested IF Else Statement")```

Within the R Nested If Else program example, If the specified person’s age is less than 18, then he is not eligible to work. If the age is greater than or equal to 18, the first condition fails; it checks the else statement. There is another Boolean expression (Nested If Else) within the Else statement.

• The R Nested If Else Statement checks whether the person’s age is greater than or equal to 18 and less than or equal to 60. If the expression is TRUE, then he can apply for the job.
• If the expression result is FALSE, he is too old to work as per the government.
• We placed one print statement outside the If Else block, which will execute irrespective of the condition result.

Please refer to the If Else statement and If Statement articles in R Programming.

OUTPUT 1: We will enter age = 35 means the first IF condition is FALSE. It will go to the else block, and within the else block, it will check the if (age >= 18 && age <=60), which is TRUE. So, it will print the statements inside this block.

OUTPUT 2: From the below R Nested If Else screenshot, you can observe that we entered the age = 14 means, the first If condition is TRUE. That’s why the statements inside the first if block are executed.

This time, we will enter age = 67 means first If condition is FALSE. It will go to the else block, and within the else block, it will check the expression if (age >= 18 && age <=60), which is FALSE. That’s why it will print the statements inside the Nested else block.

## Common Mistakes to Avoid

1. Forgetting the parentheses before and after the expression leads to a syntax error.
2. Missing curly braces after the expression. It will work when you have a single statement within the if block. However, it leads to an error for multiple statements. Apart from that, if you miss {} after the if statement, the R programming language won’t recognize the else keyword.
3. Placing the else keyword in a separate line throws an error. Always put it after the end of the if curly braces, i.e., } else.
4. Using = instead of == will throw an error.