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?
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.
– Neo Fink
As you practice more programming and enter other languages concepts so become clearer, you can be sure !
– Absolver