Assigning values to repeating variables

Asked

Viewed 96 times

0

I was doing an exercise on the Fibonacci sequence and I was quite confused by the question of assigning values to variables within repetitions. I found a solution as if by luck and ended up not understanding my own code, for the sake of confusion regarding the variables.

Here’s the code:

    algoritmo "Sequência de Fibonacci"
var
   C, PN, SN: Inteiro
inicio
      EscrevaL(" Sequência de Fibonacci (15 primeiros números) ")
      EscrevaL
      PN := 0 // Linha desnecessária, pois o valor 0 é atribuído por padrão às variáveis numéricas em Visualg.
              // Porém, por questões de entendimento eu achei melhor atribuir o valor à variável nesse caso.
      Escreva(PN) // >>> 0
      SN := 1
      Para C := 1 Ate 7 Faca
           SN := SN + PN // SN recebe último valor atribuído a si + último valor atribuído a PN
           Escreva(SN) // >>> 1, 2, 5, 13, 34, 89, 233
           PN := PN + SN // PN recebe último valor atribuído a si + último valor atribuído a SN
           Escreva(PN) // >>> 1, 3, 8, 21, 55, 144, 377
      FimPara
      EscrevaL
fimalgoritmo

Can you tell me if my comments are enlightening, or my understanding of the code is still shallow...? For example, if I were to explain to someone the following code, I would end up getting totally lost without the comments, because the idea of assigning the value of a variable to itself already leaves me a little confused. Would I be right to say that repetition creates multiple lines of code independent of each other? Or is that just what I see on the screen?

1 answer

1


Your comments regarding the code are indeed pertinent, and should be done in this way, because it is commented so that others can understand or even to you understand. No matter how much programming experience you have, it is impossible to memorize all the commands of a language, we always have to remember and search, and comments help a lot in this.

Now, regarding the assignment of the value of a variable in itself, as in:

SN := SN + PN

We can analyze the above function as a simple counter. Counters are very common in loops of repetition, serve as a condition to finalize the same. When assigning the value of a variable to itself plus another value within a loop, there is a constant increase in the value of that variable, that is, it is repeated arescented a value and this armiezenado continuously in the variable.

Another tip in case of unnecessary line: PN := 0 It is better that you remove it from there and put a comment explaining that you really do not need to assign this value, because it is already a standard. Leave the code part for practical operation, and the comments for understanding purposes.

  • I even understand that the structure serves to increase a value progressively within the loop of repetition, but I get a little confused in this logic. Anyway thanks for the reply! I think it is not always so necessary to understand the smallest details of things, I will try to continue studying programming, dominating the basics so things will be clarified.

  • 1

    As you practice more programming and enter other languages concepts so become clearer, you can be sure !

Browser other questions tagged

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