# R if Statement

The if statement consists of the Boolean expressions followed by one or more statements. The if statement is the simplest decision-making statement which helps us to take a decision on the basis of the condition.

The if statement is a conditional programming statement which performs the function and displays the information if it is proved true.

The block of code inside the if statement will be executed only when the boolean expression evaluates to be true. If the statement evaluates false, then the code which is mentioned after the condition will run.

The syntax of if statement in R is as follows:

if(boolean_expression) {
// If the boolean expression is true, then statement(s) will be executed.
}

Flow Chart

Let see some examples to understand how if statements work and perform a certain task in R.

### Example 1

x <-24L
y <- “shubham”
if(is.integer(x))
{
print(“x is an Integer”)
}

Output:

### Example 2

x <-20
y<-24
count=0
if(x<y)
{
cat(x,“is a smaller number\n”)
count=1
}
if(count==1){
cat(“Block is successfully execute”)
}

Output:

### Example 3

x <-1
y<-24
count=0
while(x<y){
cat(x,“is a smaller number\n”)
x=x+2
if(x==15)
break
}

Output:

### Example 4

x <-24
if(x%%2==0){
cat(x," is an even number")
}
if(x%%2!=0){
cat(x," is an odd number")
}

Output:

### Example 5

year
1 = 2011
if(year1 %% 4 == 0) {
if(year1 %% 100 == 0) {
if(year1 %% 400 == 0) {
cat(year,“is a leap year”)
} else {
cat(year,“is not a leap year”)
}
} else {
cat(year,“is a leap year”)
}
} else {
cat(year,“is not a leap year”)
}

Output: