What is the correct definition of concatenation and what really happens when we do this with variables?

Asked

Viewed 57 times

1

I’m learning programming on my own and I confess I’m still at the beginning. Using Portugol Studio, I learned a little about concatenation and, at first, it seemed simple. The initial concept given to me was union of two or more strings, which I took for granted, since it actually made sense for a few moments. I thought that I would have another way to perform calculations with numerical variables, since concatenation should turn them into a set of characters, but that wasn’t the case for me.

programa
{
    
    funcao inicio()
    {
        real pi = 3.14
        escreva("O número pi tem uma seríe infinita de algarismos decimais, mas pode é comumente resumido em ", pi, "\n")
        escreva("A maioria das cálculadoras científicas aproxima pi para ", pi+0.0015926,"\n")
        escreva("3,14"+"0.0015926")
    }
}

The algorithm had three different outputs:
3.14 (original value);
3.1415925 (value obtained by summing floating point numbers);
3.140.0015926 (value obtained by joining two strings).

Since concatenation is a junction of two strings, why the second output of data returned a sum of two variables of the real type and not what happened in the third?

I understand that for a variable to be transformed into a string, it should be put in quotes, but if they are not automatically transformed during a concatenation, this would break the concept of "union between two or more strings". So, what the actual concept of concatenation is and what happens when we concatenate variables?

2 answers

1


Vinicius, your own questions and code answer for you:

  • "Why the second output of data returned a sum of two variables of the real type?"
    See the code: pi+0.0015926 here no strings, there is a numeric variable (Real) and a value, then the sum is made, there is no concatenation here.
  • "And not what happened on the third?"
    See the code: "3,14"+"0.0015926" here are two strings so the concatenation happened.
  • "I understand that for a variable to be transformed into a string, it should be put between quotes, but if they are not automatically transformed during a concatenation..." this concept is partially correct, it depends on the language. Most languages will not accept something like "1" + 1 or 3.14 + "2.2", because it is not possible to concatenate the different types. This will generate an error and it will be necessary to use a conversion function, or convert the two to string to occur concatenation, or to number, to occur the sum, but some languages simply make the conversion "automatic" ai, so you need to pay attention to these cases.

On the theme of the question yes, the term "concatenation" used in the computation context is actually the process of "joining" strings, as seen on the Wiki: https://en.wikipedia.org/wiki/Concatenation

Concatenation is a term used in computation to designate the operation to merge the contents of two strings. For example, consider the strings "house" and "mento" the concatenation of the first with the second generates the string "wedding".

That is, for concatenation to occur, all values (can more than 2) must be of the string type. In your second case there are no strings, then the sum operation occurs.

  • I feel very happy that two experienced professionals answered my question almost at the same time. Getting help from both of you has doubled my spirits with the programming study. I just have one more question I think it’s only fair to ask you, since it was the first between the two answers. With the concatenation operator "," in portugol, I can do something like this: write (the result of its addition was: "resultAdicado), but the right thing would be to divide this into two data outputs, right? One for each type.

  • Thanks for the feedback @Viníciusmagalhães. About the write, the "," there is not quite an operator to concatenate, it serves to receive "a list of parameters", then a escreva("A soma é: ", x+y) works. Since it is a command/function to write in the console, internally it can even convert to string, but not before appending, so the comma is not well to concatenate, but to pass a list of parameters to be written :) good studies

1

First read this: What is a variable?.

You do not sum or concatenate or do any operation with variables, but rather do with values, which eventually are stored in variables or in objects that are referenced by variables. Without conceptualizing right can not learn properly, and it seems that wants to learn the right.

The question of difference of result would be strange for those who have never heard of concatenation, but it is self-explanatory when you know what it is.

When the operator applies + in numerical values he makes a mathematical sum that everyone knows. When the same operator is applied to the texts it makes the one of concatenation, that is, it joins two texts and creates a new one with everything together.

The second case has numerical values, why would he concatenate with it? It would be strange.

So variables are not transformed into strings or anything else, for the above.

To transform a numeric value is not put in quotes, if you put quotes then what you have there is a text, a string, is not a numerical value transformed. And if there are no quotation marks it is not a text value or another type that has a literal defined by the language.

The literal that has one or a sequence of numerical digits indicates a quantitative value. The literal that starts and ends with quotation marks indicates a description of something, a text.

When it concatenates values, and this only happens in values of the type string, each character of the second value will be put in sequence right after the existing characters of the first value. The exact way this happens in memory seems to be irrelevant at the moment since it is the most in-depth knowledge and that in general the programmer does not need to understand, until it is more advanced and wants to dominate everything.

It has language, until very popular that mess it up when it mixes a text a number, but we will work with the idea of a language that does not do crazy things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.