Processing
Types of operations
The operations to be performed with the data can be:
- Arithmetic: classic operations of addition, subtraction, multiplication and division or maths operations.
- Logics: comparisons, negation, AND, OR.
- Concatenation: union of several elements (strings of characters or variables of different types)
- Loops: Involves performing actions repeatedly. In this case it will be convenient to distinguish two types:
- Previously known number of times to repeat the action: we will use the For or Repeat structures.
- Number of times to repeat depending on values obtained: we will use the structures While (While...do) or Repeat until (Do...while), depending on whether we want to evaluate the condition before or after the first iteration. We will see that these structures are conditional as well as repetitive.
- Conditional: implies carrying out some actions or others by making decisions. Structures If-then (If-else), and Switch.
We have already seen the first three types of operations in the previous examples. We will now focus on the last two, although we will introduce various operations in the examples to delve into their use.
Iterations and loops
To practice with these structures, we will create a simple program that will calculate the average of several numbers (marks, for example). First, the program requests the number of marks to average, then it asks that the marks be entered as many times as we have told it (here is the repetition). At the end, it shows the average. In this case, since the number of iterations is known, we will use the FOR statement. Likewise, we will use arithmetic and concatenation operators.
Steps 1 and 2: Analysis and flow chart of the Average of n marks program
The elements involved are:
- Start and end of algorithm.
- Outputs: Request number of marks, show average.
- Inputs: number of marks and marks to be averaged.
- Storage: number of elements (integer), entered numbers (real number), cumulative sum of numbers (real number), and average (real number)
- Processing: loop, addition and division.
The flowchart associated with the FOR instruction is as follows:
Flowchart:
Steps 3, 4 y 5: Coding, compilation and checking of the Average of n marks program with PSeInt
As we saw in the previous section, after renaming the algorithm, we begin by defining the variables involved by writing the expression Definir
PSeInt allows you to define several variables of the same type on the same line, separated with commas. As you can see, we have defined a single numi variable where we will save the number entered by the user each time.
Next we request the number of numbers to average (Escribir), we read the response and store it in the corresponding variable (leer)
With the value of nnum defined by the user comes the time for repetition: we will ask the user to enter a number nnum times. To do this we will use the For (Para) command available in the window on the right.
When we click on the command, the following instructions are written:
As variable numérica a local variable (i, j, k...) is usually defined, which acts as a counter. The initial value is 1 and the final value is the number of times we want to repeat the instruction. The step (Paso) specifies the growth of the counter from one iteration to another (in our case one by one)
Finally, in the secuencia de acciones we have to put what we want to be repeated each time. In our case, request the number, read it and add it to the previous ones, saving the result in the accum variable.
The value of numi is refreshed and varies in each iteration, since we have already saved the value of the previous number in the accumulated one.
In a program it is very important to use the minimum number of necessary variables so as not to add unnecessary complexity to it. (KISS Principle of Software Design)
We have concatenated the numeric counter variable in the request message. This allows us to show the user what iteration they are on. The Escribir instruction followed by the elements to be concatenated separated by commas places them on the same line and without separation between them. That is why it is necessary to consider the separation spaces in the character strings that we include.
Finally, we assign the average value to the corresponding calculation and display the result again by concatenating two messages.
Finally we click Ejecutar and verify the process carried out.
Again in the verification of the program we can experience the robustness of the program against errors, introducing decimal numbers with commas, negatives, etc... We invite you to do it and enter the necessary instructions to prevent errors.
Steps 3, 4 y 5: Coding, compilation and checking of the Average of n marks program with Scratch
In Scratch the blocks related to repetitive structures are found in Control and are Forever, Repeat and Repeat until.
In our cas, as the number of iterations is defined, we will use Repetir.
First of all, in the Variables block we will define the necessary variables.
In an analogous way, using the Sensing, Looks, and Variables blocks explained in the Inputs, Outputs and Storage sections, we build the program. In Operators we will find the blocks necessary to carry out arithmetic, logical and concatenation operations.
The final program with all the elements would look like this:
Conditionals
To practice with these structures, we will carry out a simple program in which after requesting a numerical grade, and first verifying if the value received is correct, it will tell us whether the grade corresponds to a PASS or a FAIL.
Steps 1 and 2: Analysis and flow chart of the Report program
Involved elements are:
- Start and end of algorithm.
- Outputs: Request numerical mark, show textual mark or error message.
- Inputs: numerical mark
- Storage: numerical mark (real number, may be decimal)
- Processing: logical (comparison, AND, OR) and conditional
Flowchart:
In this case we have defined the error condition using the logical operator OR, indicating that any mark less than zero or greater than 10 is considered erroneous. This program can be solved in an analogous way using the operator AND, indicating that it considers any numerical mark valid greater than or equal to zero and less than or equal to 10.
Steps 3, 4 y 5: Coding, compilation and checking of the Average of n marks program with Scratch
In Scratch the blocks related to repetitive structures are found in Control and are Forever, Repeat and Repeat until.
In our cas, as the number of iterations is defined, we will use Repetir.
First of all, in the Variables block we will define the necessary variables.
In an analogous way, using the Sensing, Looks, and Variables blocks explained in the Inputs, Outputs and Storage sections, we build the program. In Operators we will find the blocks necessary to carry out arithmetic, logical and concatenation operations.
The final program with all the elements would look like this:
Steps 3, 4 y 5: Coding, compilation and checking of the Report program with Pseint
First, we rename the algorithm (Report in our case) and define the variables involved, which is only the numerical mark (nummark)
Next we request the numerical mark (Escribir) and enter it in the defined variable (Leer)
Now it comes the application of the condition. The commands in charge of entering the conditions in PSeInt are Si-entonces, Según, Mientras and Repetir. An example of the syntax of each one can be seen in the following figure
- Si-entonces: It verifies that a condition is met or not and based on this, it executes some actions. Some conditionals can be nested inside others.
- Según: It is used when a variable can adopt a discrete number of options (menu type) and the behavior is defined in relation to them and in the event that it does not match any of them.
- Mientras: evaluates a condition in a loop and while it is true executes an action. Conditional and loop mix.
- Repetir-hasta que : executes an action in a loop until a condition is met at which point it is interrupted. Conditional and loop mix.
In our example, the condition is only evaluated once, so we discard the last two, and the numerical value entered can have infinite values between 0 and 10, so the second one is not valid either. We will then use the Si-entonces command.
We are going to take the opportunity to introduce two new operators, the AND operator represented by the character & and the OR operator represented by the double slash || which are used when we want more than one condition to be evaluated simultaneously. In our case we can consider two options:
- If the mark entered is less than 0 or greater than 10, it does not make sense, and an error message must be given. Otherwise it will be necessary to distinguish (nested conditional) if it is less than 5 and then the message says FAIL, and if it is greater than or equal to 5 it will be approved.
- If the mark entered is greater than or equal to 0 and less than or equal to 10, it is correct and we must distinguish (nested conditional) if it is less than 5 or greater. Otherwise, the error message appears.
Both are right and choosing one or the other will only depend on the programmer and whether they prefer to use AND or OR operators.
Finally we would only have to run the program and verify its correct operation.
We leave it as an expansion exercise to reformulate this program so that it continues asking until it receives a valid grade. As a hint we suggest exploring the possibilities of Repetir and Mientras instructions.
Steps 3, 4 y 5: Coding, compilation and checking of the Report program with Scratch
In Scratch the blocks related to conditional structures are found in Control, next to the loops.
The logical operators AND and OR are found in Operators, along with the comparison operators and those already seen previously: arithmetic, concatenation...
As in all programs, we will start by defining the necessary variables, in our case only one from the Variables blocks.
Next we will request the rating using the block available in Sensing and we will store its response in the newly created variable.
Finally, we will evaluate the response obtained through the corresponding Control block and using the logical operators of Operators, depending on their value, we will offer the error message or write the final grade with the corresponding Looks block.
The resulting code would be the following:
In this case we use the OR operator because Scratch does not have the combined operators less than or equal and greater than or equal, and thus we save ourselves from having to combine both operators with a logical operator OR. Remember the KISS principle.
Finally, we verify the correct functioning of the program and its robustness against the most common errors. In Scratch we execute by clicking on the green flag.