R if...else

In computer programming, the if statement allows us to create a decision making program.

A decision making program runs one block of code under a condition and another block of code under different conditions. For example,

  • If age is greater than 18, allow the person to vote.
  • If age is not greater than 18, don't allow the person to vote.

R if Statement

The syntax of an if statement is:

if (test_expression) {
  # body of if
}

Here, the test_expression is a boolean expression. It returns either True or False. If the test_expression is

  • True - body of the if statement is executed
  • False - body of the if statement is skipped

Example: R if Statement

x <- 3

# check if x is greater than 0
if (x > 0) {
  print("The number is positive")
}

print("Outside if statement")

Output

[1] "The number is positive"
[1] "Outside if statement"

In the above program, the test condition x > 0 is true. Hence, the code inside parenthesis is executed.

Note: If you want to learn more about test conditions, visit R Booleans Expression.


R if...else Statement

We can also use an optional else statement with an if statement. The syntax of an if...else statement is:

if (test_expression) {
  # body of if statement
} else {
  # body of else statement
}

The if statement evaluates the test_expression inside the parentheses.

If the test_expression is True,

  • body of if is executed
  • body of else is skipped

If the test_expression is False

  • body of else is executed
  • body of if is skipped

Example: R if...else Statement

age <- 15

# check if age is greater than 18
if (age > 18) {
  print("You are eligible to vote.")
} else {
  print("You cannot vote.")
}

Output

[1] "You cannot vote."

In the above statement, we have created a variable named age. Here, the test expression is

age > 18

Since age is 16, the test expression is False. Hence, code inside the else statement is executed.

If we change the variable to another number. Let's say 31.

age <- 31

Now, if we run the program, the output will be:

[1] "You are eligible to vote."

Example: Check Negative and Positive Number

x <- 12

# check if x is positive or negative number
if (x > 0) {
  print("x is a positive number")
} else {
  print("x is a negative number")
}

Output

[1] "x is a positive number"

Here, since x > 0 evaluates to TRUE, the code inside the if block gets executed. And, the code inside the else block is skipped.


R if...else if...else Statement

If you want to test more than one condition, you can use the optional else if statement along with your if...else statements. The syntax is:

if(test_expression1) {
  # code block 1
} else if (test_expression2){
  # code block 2
} else {
  # code block 3
}

Here,

  • If test_expression1 evaluates to True, the code block 1 is executed.
  • If test_expression1 evaluates to False, then test_expression2 is evaluated.
    • If test_expression2 is True, code block 2 is executed.
    • If test_expression2 is False, code block 3 is executed.

Example: R if...else if...else Statement

x <- 0

# check if x is positive or negative or zero
if (x > 0) {
  print("x is a positive number")
} else if (x < 0) {
  print("x is a negative number")
} else {
  print("x is zero")
}

Output

[1] "x is zero"

In the above example, we have created a variable named x with the value 0. Here ,we have two test expressions:

  • if (x > 0) - checks if x is greater than 0
  • else if (x < 0) - checks if x is less than 0

Here, both the test conditions are False. Hence, the statement inside the body of else is executed.


Nested if...else Statements in R

You can have nested if...else statements inside if...else blocks in R. This is called nested if...else statement.

This allows you to specify conditions inside conditions. For example,

x <- 20

# check if x is positive
if (x > 0) {

  # check if x is even or odd
  if (x %% 2 == 0) {
    print("x is a positive even number")
  } else {
    print("x is a positive odd number")
  }

# execute if x is not positive
} else {

  # check if x is even or odd
  if (x %% 2 == 0) {
    print("x is a negative even number")
  } else {
    print("x is a negative odd number")
  }
}

Output

[1] "x is a positive even number"

In this program, the outer if...else block checks whether x is positive or negative. If x is greater than 0, the code inside the outer if block is executed.

Otherwise, the code inside the outer else block is executed.

if (x > 0) {
  ... .. ...
} else {
  ... .. ...
}

The inner if...else block checks whether x is even or odd. If x is perfectly divisible by 2, the code inside the inner if block is executed. Otherwise, the code inside the inner else block is executed.

if (x %% 2 == 0) {
  ... .. ...
} else {
  ... .. ...
}
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges