## R If AND

R If statement has a condition which evaluates to a boolean value, and based on this value, it is decided whether to execute the code in if-block or not. Now, this condition can be a simple condition or a compound condition. A compound condition is formed by joining simple conditions using logical operators.

In this tutorial, we will learn how to use logical AND operator `&&`

in R If statement.

### Syntax

The syntax to use logical AND operator in if-statement to join two simple conditions: condition1 and condition2 is

if(condition1 && condition2){ #code }

The overall condition becomes TRUE only if both condition1 and condition2 are TRUE.

The truth table for different values of simple conditions joined with AND operator is

condition1 | condition2 | condition1 && condition2 |
---|---|---|

TRUE | TRUE | TRUE |

TRUE | FALSE | FALSE |

FALSE | TRUE | FALSE |

FALSE | FALSE | FALSE |

### Examples

In the following program, we take a value in x, and check if this value is greater than zero and even number. Now, there are two simple conditions here. The first is if x is greater than zero. The second is if x is even. We need a condition where these two conditions are TRUE. Therefore, we shall use AND operator to join these simple conditions in our IF statement.

**example.R**

x <- 10 if (x > 0 && x%%2 == 0) { print("x is greater than 0 AND x is even.") }

**Output**

[1] "x is greater than 0 AND x is even."

Now, let us take another value for x, where the second condition is FALSE.

**example.R**

x <- 9 if (x > 0 && x%%2 == 0) { print("x is greater than 0 AND x is even.") } print("end of program")

Since, the whole if-condition evaluates to FALSE, the code inside if-block does not execute.

**Output**

[1] "end of program"

### Conclusion

In this R Tutorial, we learned how to use logical AND operator in IF statement in R Programming language, with the help of example programs.