Computational Concepts I

In the following you find some explanations for three out of the seven concepts that make up a program. The concept was developped for teaching Computer Science to children using Scratch, here is the original source for reference.

In the following I describe four out of the seven computational concepts.

Sequence

All steps of a program are executed one after the other in a given order.

 

Loops

Programs often need to repeat certain tasks, sometimes a definite number of times, sometimes things need to be repeated until the program ends.

If actions are repeated a a known number of times, you use the “repeat … times do” block. You can enter the number of repetitions between “repeat” and “times”.

Sometimes you want to repeat something until the end of the program. If you want to monitor the temperature permanenty, you would use this type of loop. For this application you will use the “while (true) do” block. the “true” block can also be replaced with an operator returning true or false values. See below for logical operator

The other two type of loops will explained later, they allow you to scan through a list of variables, sometimes they are also referred to as array.

mb Loops

Data

Programs use variables to store, retrieve and update values. You can think of a variable as a box. The box has a label to identify it and its contents. The label is the name of the variable. In the picture below you I have declared two variables, named “item” and “newVar”.

When clicking on the small triangle/flash, you can select a different variable or box. But first you need to declare a new variable by clicking on “Make a Variable”.

With the “set (item) to (…) you assign a value to that variable. In the context of the box, you put something (a number, a textstring, …) into the box. You assign a value by entering something in the (…) field with the keyboard. But you can also attach another variable block or an operator block to assign a value to a variable. An operator block performs a calculation and gives the result to the block it is attached to. Have a look below in the next section “Conditionals and logical operators“.

You can also change the content of a variable using the “change (item) by (…) ” block.

mb VariablesBlockly

Conditionals and logical Operators

Conditionals (if blocks) allow to check values and states. They are working in the following way:

If (something is true?) then (do something), else (do something different). The else statement is optional, so you do not need to use it.

The part (something is true?) is done by a logical operator that is returning one of two values: True or False.

Let’s assume as an example that the operator is (i < 4). i is a variable.

If i has the value 5, the logical operator will return false (5<4 => false), when (i has the value 2, it will return a true (2<4 => true).

Depending on the answer to the if statement, the program can do different things. You can also put one operator inside another operator, nested blocks. An example is given below. Have a look.

mb Conditional1

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.