logo

CONDITIONAL STATEMENT IN R


Conditional statements are decision making structure which are tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.




The general flow chart of a conditional structure is:

R provides the following types of decision making statements:

if satement:

The basic syntax for creating an if statement in R is −

if(conditional_expression) {
    statement(s) will execute if the conditional expression is true.
}

If the conditional expression evaluates to be true, then the block of code inside the if statement will be executed. If conditional expression evaluates to be false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

The flow diagram for if statement is:

An example of if statement is:

x=20
if (x<30) {
  print("Cotton University")
}
[1] "Cotton University"

if-else statement:

Here an if statement is followed by an optional else statement which executes when the conditional expression is false.

The basic syntax for creating an if…else statement in R is −

if(condition_expression) {
    statement(s) will execute if the condition expression is true.
} else {
    statement(s) will execute if the condition expression is false.
}

If the condition expression evaluates to be true, then the if block of code will be executed, otherwise else block of code will be executed.

The flow diagram of if-else statement is:

An example of if-else statement:

marks=c(15,45,65,78,13,45,8,60)

for (i in 1:8) {
        if (marks[i]<30) {
            print("FAIL")
}
        else {
            print("PASS")
        }
}
[1] "FAIL"
[1] "PASS"
[1] "PASS"
[1] "PASS"
[1] "FAIL"
[1] "PASS"
[1] "FAIL"
[1] "PASS"

An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.

When using if, else if, else statements there are few points to keep in mind.

The basic syntax for creating an if...else if...else statement in R is −

if(condition_expression 1) {
    Executes when the condition expression 1 is true.
} else if( condition_expression 2) {
    Executes when the condition expression 2 is true.
} else if( condition_expression 3) {
    Executes when the condition expression 3 is true.
} else {
    Executes when none of the above condition is true.
}

An example of nested if-else statement:

data=c(15,45,65,78,13,45,8,60)
for (i in 1:length(data)) {
  if(data[i]<30){
    print("FAIL")
    }
  else if(data[i]>=30 && data[i]<45){
    print("Third division")
  }
  else if(data[i]>=45 && data[i]<60){
    print("Second division")
  }
  else {
    print("First division")
    }
}
[1] "FAIL"
[1] "Second division"
[1] "First division"
[1] "First division"
[1] "FAIL"
[1] "Second division"
[1] "FAIL"
[1] "First division"

switch statement:

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

The basic syntax for creating a switch statement in R is −

switch(expression, case1, case2, case3....)

The following rules apply to a switch statement −

The flow diagram of switch statement is

An example of switch statement:

x=c(1,3,5,7,9)
test=1
test_result=switch (test,
  mean = mean(x),
  med = median(x),
  max = max(x)
)
test_result
[1] 5





NOTE:

Here the switch statement is use to switch between mean, median and max functions. The test variable determines which function inside the switch statement is to executed.