Introduction Objectives
- Think computationally
- Use logic
- Solve problems
- Instruct computer to do something
Block programming and R implementation
Learning programming and coding might seem daunting, but it really boils down to being able to define specific steps needed to solve a problem or accomplish a task. This involves using logical thinking and problem solving, including sequencing and conditional logic, to assemble a step-by-step solution. Weβll use block programming to explore this.
Block programming is a method of learning the principles of coding via a visual
interface. Learners drag and drop blocks of instructions into the order they
want them to be executed, all without having to learn a programming language.
It will get you familiar with thinking computationally before learning a
scripting language such as R.
For example, the following instructions can be put together into a program:
- let
my variable
refer to a real integer between 1 and 20 - let
if
condition test whethermy_variable
is less than 10 - if
my variable
satisfies theif
condition, add two tomy variable
- print
my variable
- if
my variable
does not satisfy theif
condition, setmy variable
to 100 - print
my variable
The following is a script in the R programming language that implements the instructions above.
my_variable <- sample(1:20, 1)
if (my_variable < 10) {
my_variable <- my_variable + 2
print(my_variable)
} else {
my_variable <- 100
print(my_variable)
}
If you understand the instructions above, then you know what the R code is accomplishing even though you canβt read the R language. However, you can probably deduce what a lot of it is doing!
-
What do you think the combination of characters
<-
means? What about the pattern{...}
? -
Which step is implemented by the
if (...) {...}
block? -
What is the role of
my_variable
?
Block programming Exercise 1
Go to the block programming platform Scratch and use the instructions above to assemble a program that prints a number.
For fun, see if you can create a program that makes the cat walk across the screen!
Block programming Exercise 2
Complete the following instructions to test whether a given
integer is even or odd. Assume you can use a pre-existing capability to round
any number to its nearest integer, as well as the arithmetic operators *
and
/
.
- let
i
refer to a given integer - if
i
is less than zero, leti
refer to-1 * i
β¦
Block programming Exercise 3
One common task in programming is to do a series of steps many times, which is called a loop in coding.
In the Scratch platform, create a program that
calculates the square root of each number from 1-20, and if the result is an integer, print the square root.
If you need to catch-up before a section of code will work, just squish it's π to copy code above it into your clipboard. Then paste into your interpreter's console, run, and you'll be ready to start in on that section. Code copied by both π and π will also appear below, where you can edit first, and then copy, paste, and run again.
# Nothing here yet!