Help a little with delphi loop

 for m := 0 to 300 do
       if Pos(certain_String, string(arrayitem(m)) <> 0 then
        begin
          randomize;
          x := random(100);
        begin
            case x of
             0..69  : function(m); //  70 percent
            70..79 : function(m+1); // 10 percent
            80..84 : function(m+2); // 5 percent
            85..88 : function(m+3); //  5 percent
            89..91 : function(m+4); //  2 percent
            92..94 : function(m+5); //  3 percent
            95..97 : function(m+6); //  3 percent
            98..98 : function(m+7); //  1 percent
            99..99 : function(m+8); //  1 percent


           end;
           m := 300;

    end;

      

Trying to make this thing loop on a rather large array, but if it finds a specific string in it, stop completely and move on to the next thing, had to take out some proprietary function names, but m: = 300; seems to be a problem, I don't like it when I assign a value to m in the middle of the for loop. Sorry for lack of knowledge here, got a half inch pascal project that fell into my lap and never even seen it before.

0


source to share


4 answers


Changed the code a bit:

randomize;
for m := 0 to 300 do begin
  if Pos(certain_String, string(arrayitem(m)) <> 0 then begin
    x := random(100);
    case x of
       0..69 : function(m); //  70 percent
      70..79 : function(m+1); // 10 percent
      80..84 : function(m+2); // 5 percent
      85..88 : function(m+3); //  5 percent
      89..91 : function(m+4); //  2 percent
      92..94 : function(m+5); //  3 percent
      95..97 : function(m+6); //  3 percent
          98 : function(m+7); //  1 percent
          99 : function(m+8); //  1 percent
    end;
    break;
  end;
end;

      

Will do the trick.



Do not randomize every cycle.

And why is the line executed?

+3


source


Among other issues, the way to terminate a for loop is Breaking . Just replace m: = 300; on Break; and it will do what you want.



Also, calling randomization more than once is a bad idea, but you know that now since it was mentioned in the answer you accepted yesterday for this problem.

+3


source


There must be a C equivalent in Object Pascal break

. This is a cleaner way to exit the loop.

0


source


An example with a while loop:

m := 0;

while m<=300 do begin
  if condition do begin
    // Do something

    m := 300; // Will be 301 at end of loop.
  end;
  Inc(m);
end;

      

0


source







All Articles