# R If Else Statement

If statement

It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if statement’.

Syntax:

```if (Boolean_Expression)  {
Statement 1;
Statement 2;
………….
………….
Statement n;
}```

First of all, it will check the given expression or test condition, if it is true then all the statements within the block (statement 1, 2…. n) will be executed, else all those statements will be skipped.

Flow Chart: Example:

```a <- 100
b <- 10
if (a > b){
print ("A is greater than b")
}```

Output:

` "A is greater than b"`

if…else statement

If the condition is true, then if the block is executed. But if the condition is false, then else block is executed.

Syntax:

```if (Boolean_Expression)  {
Statement 1;
Statement 2;
………….
………….
Statement n;
} else {
Statement 1;
Statement 2;
………….
………….
Statement n;
}```

Flow Chart: Example:

```a <- 100
b <- 200
if (a > b){
print ("a is greater than b")
} else{
print ("b is greater than a")
}```

Output:

` "b is greater than a"`

if…else if…else statement

It is an extension of if…statement. If the condition is true, then the statement of if block will be executed, else another if…else statement is evaluated. You may append any number of an if…else statement followed by one to another.

Syntax:

```if(boolean_expression 1) {
//Statement1
} else if( boolean_expression 2) {
//Statement2
} else if( boolean_expression 3) {
//Statement3
} else {
//Statement4
}```

Flow Chart: Example:

```x <- -10
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else
print("Zero")```

Output:

` "Negative number"`