Conditionals

10 > 5
"aang" == "aang"
3 != 3
5 > 2 & 6 >=10
5 > 2 | 6 >=10

Do Exercise 1 - Choice Operators.

Discuss floating point with students.

  • Did you notice anything weird while you were doing the exercise?
  • Take a closer look at item 4.

Floating point

> x <- 1.3
> y <- 2.8
>  x * 2 + 0.2 == y
[1] FALSE
> 1.3 * 2 + 0.2
[1] 2.8
>>> 1.3 * 2 + 0.2 == 2.8
False
>>> 1.3 * 2 + 0.2
2.8000000000000003

if statements

superhero <- "Batman"
if (superhero == "Batman") {
  print("That is correct")
  }
print("Done")
superhero <- "Black Widow"
if (superhero == "Batman") {
  print("That is correct")
} else {
  print("Please try again")
}
print("Done")
if (superhero == "Batman") {
  print("That is correct")
} else if (superhero == "Black Widow") {
  print("OK, you may have a point")
} else {
  print("Please try again")
}
print("Done")
superhero <- "Flash"

Convert to function

is_best_superhero <- function(superhero) {
  if (superhero == "Batman") {
	print("That is correct")
  } else if (superhero == "Black Widow") {
	print("OK, you may have a point")
  } else {
	print("Please try again")
  }
}

is_best_superhero("Hulk")
is_best_superhero <- function(superhero) {
  if (superhero == "Batman") {
	print("That is correct")
  } else if (superhero == "Black Widow" | superhero == "Hulk") {
	print("OK, you may have a point")
  } else {
	print("Please try again")
  }
}

is_best_superhero("Hulk")

Do Exercise 2 - Complete the Code.