Now that we know what algorithms are, we can continue learning other basics of programming that we can find in most languages. Remember that an algorithm is a problem solving procedure which consists of a well-defined, ordered, and finite sequence of instructions. To execute all these steps in the order we want, there are different control structures, such as conditionals or loops.

Conditionals and loops

Conditionals are structures which allow us to choose between performing one action or another. As its name implies, it is a condition and we can express it within our code as IF. On our platform, Karel’s Adventure helps students to understand it easily. In this adventure we have to guide a virtual robot through a maze and we can let this robot “choosing” what to do using conditionals. For example, we can tell him using his language (programming): “if you find a wall, turn right.” But what should Karel do if there is no wall? In this case, we could also use an ELSE to give it another instruction and indicate it, for example: “otherwise (if you can’t find a wall), go ahead.”

It should also be noted that there are three types of conditional: simple, alternative, and chained. The first one is the simplest, the first conditional we have seen: if the condition happens, we do the action that is associated with the conditional (otherwise, we do nothing). The alternative is a conditional in which we have to do something if the condition happens, but when it doesn’t happen we also need to do something different. Finally, the chained conditional is the one that is used when there are several possibilities and, for each of them, some action must be done.

There are different instructions for including conditionals in our code, such as iterative structures like WHILE, one of the most used loops in programming. Loops are sequences of cyclic instructions that allow us to repeat an action and do not stop until a condition is met. While they present some complexity, loops save us time and lines of code, making it easier for us to have a structured code that is easier to modify.

Variables and functions

Another basic element we need to know are variables, one of the most powerful mechanisms we have at our disposal when programming. A variable is the ability of a program to store information in computer memory with the intention of reusing it later. Formally speaking, a variable is a name that refers to a value.

There is an example that can help us understand this idea: the memory of a computer is like a chest of drawers for our socks (one with billions of drawers!). In order to organize this chest of drawers as best as possible, we have decided that there will be only a pair of socks in each drawer and that, in order to locate them quickly, we will put a label with an identifying name on the outside of each drawer. Thus, when we want to find our socks, we will have to look in the chest of drawers (memory) that label which identifies the socks we want to find. In the case of a variable, the process we have now described would be carried out through the assignment operation that relates a variable name (the drawer label) and a value (the socks). In a program, each time we want to use the value referenced by the variable, we simply have to type the name of the variable.

Finally, if we are talking about structuring code, we should also talk about functions. Whenever we have defined what computational thinking is we have always mentioned decomposition, which allows us to transform a large problem into different smaller ones. Programmers use this technique to structure programs and avoid writing too long codes; they write pieces of code separately and each of these pieces performs specific tasks or calculations.

These general parts or instructions that contain more specific instructions are called functions and are defined differently depending on the programming language used. There are different types of functions and we will not go into detail today, but we can conclude that they are a very useful resource to sort instructions, be able to repeat them quickly and make easier to modify our code if needed.

On its operation, functions are made up of two parts, function definition and function call. The definition is the part where we explain to the language how to carry out an operation, while the function call is when we tell the language that we want to execute the operation we have defined before: when we learn to add at school, we are taught how to perform an operation (the addition) and this process is stored in our brain associated with a keyword: ADDITION (which is the name of the function). However, we don’t spend all day adding up; we only do this if needed. Storage in our brain is the definition and the call is when someone asks us what is the sum of two numbers.

At Codelearn, we work all these concepts with children and adolescents progressively and through different programming languages, so that we can ensure that students internalize the basics of programming and acquire essential knowledge which allows them to learn new languages. Find out more about the method or contact us if you need more information about the program.