For is not giving continue (Firemonkey)

Asked

Viewed 63 times

-1

I’ve literally been looking at this piece of code since yesterday trying to figure out why the foris not passing 0. I have tried several ways, and already confirmed that I am always adding one more number to it, but simply it stops at 0.

What I want to do is this: I will always add a sequence of numbers within a Tedit and then press to send them. Only the numbers entered can never be smaller or equal to the numbers previously sent.

Can someone give me a hand? I’m using Delphi RIO and I’m developing an app for ANDROID

Here is my code:

procedure TF_dianteira.bt_OKEClick(Sender: TObject);
var
  i: integer;
  hora_texto: string;
  funcionou_menor: string;
  funcionou_igual: string;
begin
  funcionou_menor := 'sim';
  funcionou_igual := 'sim';
  ShowMessage('vai ate ' +IntToStr(Emb_Maximo));
  for i := 0 to Emb_Maximo do
  begin
    ShowMessage(IntToStr(i));
    if (StrToInt(txt_embarque_d.Text) < vetor[i]) then
    begin
      ShowMessage('Numero: ' +txt_embarque_d.Text);
      ShowMessage('Numero anteriormente digitado: ' + IntToStr(vetor[i]));
      ShowMessage('Não pode haver senhas menores que anteriores');
      funcionou_menor := 'nao';
      txt_embarque_d.Text := '';
      break;
    end
    else
    begin
      funcionou_menor := 'sim';
    end;
    if (StrToInt(txt_embarque_d.Text) = vetor[i]) then
    begin
      ShowMessage('Não pode haver senhas iguais à anteriores');
      txt_embarque_d.Text := '';
      funcionou_igual := 'nao';
      break;
    end
    else
    begin
      funcionou_igual := 'sim';
    end;

    if (funcionou_menor = 'sim') then
    begin
      if (funcionou_igual = 'sim') then
      begin
        if i <> 0 then
          SetLength(vetor, Length(vetor));

        vetor[Emb_Maximo] := StrToInt(txt_embarque_d.Text);
        ShowMessage('Vetor adicionado ' +IntToStr(vetor[Emb_Maximo]) );

        //Aqui eu vou criar um arquivo

        txt_embarque_d.Text := '';
        Emb_Maximo := Emb_Maximo + 1;
        break;
      end;
    end;

    continue
  end;    

end;
  • It’s been a while since I stopped programming on pascal Object, but if I’m not mistaken You need to force the increment of your variable "i", something like ... continue; i = i + 1;, I can’t remember but try it there

  • I tried to put one continue there at the end, give a look to you see, but remains the same problem = and i := i+1; does not work because it says that I am setting value to a variable of a for

  • with what value starts var Emb_maximo ? because it’s only increasing it in some situations... so if it starts at 0, it probably only does 1 or 2 iterations...

  • It starts with 0, but only increases if you’ve actually added some value, that’s the intention you know? for example, if I inserted "1", in the future, if I insert "1" I cannot give. but if I insert "2", you have to give, then Emb_maximo has to increase a value because you added one more in the vector

  • It’s hard to understand your code. There are global variables, so you can’t understand the context. But, it has a few things: if i <> 0 then Setlength(vector, Length(vector)); Do you notice that this code does nothing? Emb_maximo := Emb_maximo + 1; . continue remove this line, this command only makes sense if you need to go back to the beginning of the looping in the middle of processing, ignoring additional lines. in this case, there are no additional lines to ignore, as the command is at the end of the looping.

  • if i< > 0 then setLength is because I just want to increase the size of the vector after the for is no longer 0

  • before the is assigned 0 or the start value of its variable i, and at the beginning of the for an inc(i) to add one each time it goes through the for.

Show 2 more comments

1 answer

0


This is happening because the TO of FOR is evaluated only the first time, so if its variable Emb_Maximo start with zero, even increasing it within the FOR, she will not be reevaluated and her FOR will be finalized.

Think of the repeating structure FOR is mainly used when you know the beginning and the end of the loop, as its loop is dynamic, can be changed during execution, recommandd you use a WHILE, where expression is always revived, thus becoming dynamic.

Take this example:

program Hello;
var i: integer;
    Emb_Maximo: integer;
begin
  //Começa com ZERO, assim como seu exemplo
  Emb_Maximo := 0;

  for i := 0 to Emb_Maximo do
  begin
    writeln(i);
    //Mesmo aumento o valor dentro do for, ele não será reavaliado
    Emb_Maximo += 2;
  end;

  writeln(''); 
  writeln('--'); 
  writeln('');

  //Começa com DOIS
  Emb_Maximo := 2;

  for i := 0 to Emb_Maximo do
  begin
    writeln(i);
    //Mesmo aumento o valor dentro do for, ele não será reavaliado
    Emb_Maximo += 3;
  end;

  writeln(''); 
  writeln('--'); 
  writeln('');

  //Se o seu for é assim dinâmico, faça com WHILE
  Emb_Maximo := 0;
  i := 0;

  while i <= Emb_Maximo do
  begin
    //Apenas para evitar o loop infinito, poderia ser uma condição, algo solicitado ao usuário
    if i = 0 then
    begin
      Emb_Maximo += 4;
    end;

    //Mesmo começando em ZERO, a expressão é reavaliada, com isso o LOOP é executado conforme a alteração do Emb_Maximo
    writeln(i);
    inc(i);
  end;

end.

Run online: https://rextester.com/QNBV94804


This condition of FOR is language-specific, some reevaluate the condition of the FOR, such as javascript:

let Emb_Maximo = 0;

for(let i =0; i <=Emb_Maximo; i++) {
  console.log(i);

  if (Emb_Maximo == 0) {
    Emb_Maximo += 2;
  }
}

Browser other questions tagged

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