Problem with the sum of 10 numbers with for

4

I have a question in an algorithm that adds 10 numbers with the for repeat command.

soma_numero := soma_numero + idade;

When I declare soma_numero as 0 before for the algorithm runs normally, but its I do not declare 0 as it prints an unexpected value.

begin
  soma_idade := 0;
  for cont := 1 to 10 do
    begin
      writeln('Digite a idade ', cont);
      readln(idade);
      soma_idade := soma_idade + idade;
    end;
  writeln('A soma das idades e ', soma_idade);
  readln;
end.
    
asked by anonymous 17.06.2018 / 19:59

1 answer

1
  

Answer given in comments and accepted as correct:

Delphi is very weak with memory management, without initializing the variable it may receive "garbage" from memory.

    
28.09.2018 / 15:21