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?
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.
– Vinícius Magalhães
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– Ricardo Pontual