__IF Else Condition in R__

__IF Else Condition in R__

If Else conditional statements are important part of any programming so as in R. In this tutorial we will have a look at how you can write a basic IF Else condition in R. It is aimed at beginners

### R if statement

The syntax of R if statement is:

if (expression)

{

statement

}

{

statement

}

If the **expression** is **TRUE**, the statement gets executed. But if it’s **FALSE**, nothing happens.

#### Example: if Condition in R

x <- 10 if(x > 0) { print("This is Positive number") }

**Output**

[1] “This is Positive number”

### if else statement in R

The syntax of if…else statement in R is:

if (expression)

{

statement1

}

else

{

statement2

}

{

statement1

}

else

{

statement2

}

The else part is evaluated only when **expression** is **FALSE**.

x <- -10 if(x >= 0) { print("This is Non-negative number") } else { print("This is Negative number") }

**Output**

[1] “This is Negative number”

** **

### Nested if else statement in R

In nested **if… else** statements we can impose as many **else if** conditions as we require

The syntax of nested if…else statement is:

if (expression1)

{

statement1

}

else if (expression2)

{

statement2

}

else if (expression3)

{

statement3

}

else

statement4

{

statement1

}

else if (expression2)

{

statement2

}

else if (expression3)

{

statement3

}

else

statement4

__ __

#### Example of nested If Else condition in R

x <- 5 if (x < 0) { print("This is Negative number") } else if (x > 0) { print("This is Positive number") } else print("This is Zero")

**Output**

[1] “This is Positive number”