12 Conditional statements

12.1 If

Format: if (condition) statement

  • condition: expression returning a logic value (TRUE or FALSE)
  • statement: any valid R statement
  • statement only executed if condition is TRUE
## Negative

12.2 Else

Format: if (condition) statement1 else statement2

  • condition: expression returning a logic value (TRUE or FALSE)
  • statement1 and statement2: any valid R statements
  • statement1 executed if condition is TRUE
  • statement2 executed if condition is FALSE
## Negative
## Positive

12.3 Code blocks

Suppose you want to execute several statements within a function, or if a condition is true

  • Such a group of statements are called code blocks
  • { and } contain code blocks
## First is greater than second
## Their difference is  3