Adding flexibility to the conversation

Learning Objectives

  • to understand the use of conditionals
  • to learn that repeated commands can be replaced by loops
  • to understand the use of functions



Table of Contents



Introduction

Now we know a bit about the different data types and data structures that computers can understand. The next step is to learn about programming concepts.



Conditionals

With each data structure in the previous section, you've seen a piece of code used to create an example of that data structure. You know that we can create data types and store them with names as variables. But what if we wanted to query them. What if we only wanted to run our code if certain conditions were met? Such as I only want to analyse the data for participants that have no missing values, or only for people over the age of 75, or only for people that have brown hair? This is where conditionals come into the picture.

Have you ever played the game 'Simon Says'? Briefly, how the game works is that one person gives instructions. The rest of the people in the room are meant to follow the instructions if they were preceded by the words 'Simon says' and ignore the instructions otherwise. Anyone who performs an instruction that was not preceded by the words 'Simon says' is out of the game.

The game 'Simon Says' is a form of conditional. The person giving the instructions is acting as the programmer and the rest of the people in the room are acting as the computer. The condition to be met is that the programmer says the words 'Simon says'. This can be evaluated to TRUE (in the case where the programmer did say 'Simon says') or FALSE (in the case where the programmer didn't say 'Simon says'). The instructions for the computer to follow are different depending on whether the conditional was TRUE (follow the instruction) or FALSE (ignore the instruction).


Challenge 1

This time I will be the programmer and you will be the computer. The rules are:

  • If the number is greater than 5, answer: GREATER THAN 5
  • If the number is not greater than 5, answer: NOT GREATER THAN 5

What is your answer for the following numbers?

A. 6

B. 8

C. 4

D. 5


Take home messages for conditionals

  • Conditionals begin with the word 'if' followed by a condition
  • The condition evaluates to a logical value (TRUE or FALSE)
  • There are separate instructions to follow for TRUE and FALSE conditions


Here is an example of a conditional written in R to check whether I should eat the tim tam. If I’m hungry (TRUE) then the computer will print out "GIVE ME A TIM TAM!!" If I’m not hungry (FALSE), the computer will tell me to politely decline the tim tam.

hungry <- TRUE

if (hungry){
  print("GIVE ME A TIM TAM!!!")
} else {
  print("No tim tam for me, thanks :)")
}
[1] "GIVE ME A TIM TAM!!!"
hungry <- FALSE

if (hungry){
  print("GIVE ME A TIM TAM!!!")
} else {
  print("No tim tam for me, thanks :)")
}
[1] "No tim tam for me, thanks :)"



Loops

One way that we can write code is to spell out each instruction one by one. However, sometimes this gets a bit repetitive... If we wanted to print the numbers 1:10, we could say print 1, print 2, print 3, print 4, etc. Or if we want to analyse patient data, we could say analyse data for patient 1, analyse data for patient 2, analyse data for patient 3, etc. If you have several hundred patients, that would end up being a lot of copy and paste! This is where loops come in handy.

When you’re repeating the same instructions - such as print or analyse - over different data, you can use a for loop. The same instructions are repeated but the data used is different for each iteration of the loop. An example of this would be asking for a person's name. The question ('What's your name?') will be the same every time, but the result will be different depending on the person that you ask! (Unless they are all called Barry, in which case you should consider broadening your friendship group).


Challenge 2

Can you think of any repetitive tasks that you do that could be described by a for loop?


Take home messages for loops

  • Loops are used to repeat a single set of instructions over a collection of data
  • Loops require a lot less typing (or copying and pasting) than writing out the instructions separately for each element in the collection of data


Here is an example of the two step dance - universally popular with uncoordinated people - written in R. The steps have been repeated 4 times. Notice how much shorter the written code is compared to the output.

for (beats in 1:4){
  print("bring left foot to right foot")
  print("step left foot back to original position")
  print("bring right foot to left foot")
  print("step right foot back to original position")
}
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"



Functions

The last concept covered in this section is functions. Functions are a series of instructions that have been given a name (yes, that means that they are variables!). Whenever we want to run that set of instructions we can call the function by name. This is again useful to reduce the amount of copy and paste required when repeating instructions.

An example might be that you are constantly wanting to convert units from Celsius to Fahrenheit. You could store the formula as a function called convertCelsiusToFahrenheit and then call it each time you want to make that conversion. By storing the formula as a function, you don't need to remember the details of the formula anymore; you just need to remember the function name.


Tip: Function scope

Note that you can’t access the instructions inside a function when you call it by name. You only get the output. So the temperature conversion function will convert temperatures for you, but not give you the temperature conversion formula.


We use functions all the time! When you walk across the room, you are giving your brain the command to walk. But you don't consciously tell it which muscles need to be activated for that to happen. In this case the function would be named walk and the series of instructions required to walk would be stored in that function.

Another function can be found in cooking. When following a recipe, cookbooks often assume knowledge. They assume that you will understand that dice means take a knife and cut ingredients into small cubes. In this case the function name is dice and the instructions are to take a knife and cut ingredients into small cubes.

Challenge 3

You can also artificially create functions. Let’s create a function called nameTheSinger. What would the output of nameTheSinger be for the following input arguments?

A. Story of my life

B. Hit me baby one more time

C. Piano man


Take home messages for functions

  • Functions contain a set of instructions that can be called by name
  • Putting instructions in a function can save a lot of copying and pasting!


Here is an example of a function called moodCalendar written in R. I have give you this function so that you can check what my mood is likely to be on any given day. You will then be able to make an informed decision as to whether you want to talk to me that day... It looks like Friday is a better option than Monday!

moodCalendar <- function(day){
  if (day %in% c("Friday", "Saturday", "Sunday")){
    print(paste(day, "is a happy day"))
  } else if (day %in% c("Monday", "Tuesday")){
    print(paste(day, "is a grumpy day"))
  } else {
    print(paste(day, "is a neutral day"))
  }}
moodCalendar("Monday")
[1] "Monday is a grumpy day"
moodCalendar("Friday")
[1] "Friday is a happy day"



Challenge solutions

Solution to Challenge 1

A. GREATER THAN 5

B. GREATER THAN 5

C. NOT GREATER THAN 5

D. NOT GREATER THAN 5

Solution to Challenge 3

A. One Direction

B. Britney Spears

C. Billy Joel

(You should not be ashamed for knowing the answers to any of these songs!)

results matching ""

    No results matching ""