##############################################
#  Conditionals and control flow
##############################################

## --------------------------------------------------------------------------------------------------------------------------------------------------
3 == (2 + 1)
"intermediate" == "r"
(1 + 2) > 4
katrien <- c(19, 22, 4, 5, 7)
katrien > 5

## --------------------------------------------------------------------------------------------------------------------------------------------------
TRUE & TRUE
5 <= 5 & 2 > 3

## --------------------------------------------------------------------------------------------------------------------------------------------------
FALSE | TRUE
3 < 3 | 7 < 6

## --------------------------------------------------------------------------------------------------------------------------------------------------
! TRUE
! 5 > 10

## --------------------------------------------------------------------------------------------------------------------------------------------------
katrien <- c(19, 22, 4, 5, 7)
jan <- c(34, 55, 76, 25, 4)
katrien > 5 & jan <= 30

## --------------------------------------------------------------------------------------------------------------------------------------------------
num_attendees <- 30

if (num_attendees > 5) {
  print("You're popular!")
}

## --------------------------------------------------------------------------------------------------------------------------------------------------
num_attendees <- 5

if (num_attendees > 5) {
  print("You're popular!")
} else {
  print("You are not so popular!")
}

## --------------------------------------------------------------------------------------------------------------------------------------------------
x <- -2

if (x^2 < 1) {
  x^2 
} else if (x >= 1) {
  2*x-1
} else {
  -2*x-1
}

## --------------------------------------------------------------------------------------------------------------------------------------------------
ifelse(x > 0, x, -x)

## --------------------------------------------------------------------------------------------------------------------------------------------------
type_of_summary <- "histogram"
x_vector <- rnorm(n = 200, 0, 1)

switch(type_of_summary,
       mean = mean(x_vector),
       median = median(x_vector),
       histogram = hist(x_vector),
       "I don't understand")

## --------------------------------------------------------------------------------------------------------------------------------------------------
primes <- c(2, 3, 5, 7)

# loop version 1
for (p in primes) {
  print(p)
}

# loop version 2
for (i in 1:length(primes)) {
  print(primes[i])
}

## --------------------------------------------------------------------------------------------------------------------------------------------------
todo <- 64

while (todo > 30) {
  print("Work harder")
  todo <- todo - 7
  print(todo)
}

## Your Turn!
## --------------------------------------------------------------------------------------------------------------------------------------------------
for(num in ___) {
   print(num)
   ___ (num %% 2 == 0 ___ num %% 3 == 0) {
     print('Divisible by 2 and 3')
   } ___ (num %% 2 == 0 ___ num %% 3 == 0) {
     print('Divisible by 2 or 3')
   } ___ {
     print('Not divisible by 2 or 3')
   }
}






## --------------------------------------------------------------------------------------------------------------------------------------------------

