There are some clever solutions that were just proposed. We are getting somewhere
- @Kays: defining a custom enumerator is satisfactory from a meaning point of view, but it is much stuff to write and I am a bit lazy
- @Fred vS: that's an elegant solution and I would probably choose to do that when that's possible, though it requires a value. What to do if the value is an interface? Maybe create a dummy interface that would pass the test.
- @Martin_fr: this solution seems to be actually the best, it works in all cases, no duplication of code. I would suggest an out parameter to avoid to have to initialize a.
function GetValue(out val: integer): boolean; inline;
begin
val := random(101);
result := val <= 75; // test is defined here, that's no duplication
end;
......
var a: integer;
begin
writeln('here are random percentages <= 75%');
randomize;
while GetValue(a) do
writeln(a, '%');
end.
- @eny: haha the goto is a way to do something when the language structure is not available, so well not that satisfactory for me. The function that returns the value is clever, it works. There is some duplication of the value, but in this case I guess that would be ok.