0

Both

while MyFunction(1, 2, 3) > 0 do
  begin
    Temp := MyFunction(1, 2, 3);
    ShowMessage(IntToStr(Temp));
  end;

and

Temp := MyFunction(1, 2, 3);
while Temp > 0 do
  begin
    ShowMessage(IntToStr(Temp));
    Temp := MyFunction(1, 2, 3);
  end;

violate the DRY principle because there are two calls to MyFunction when only one is necessary.

finefoot
  • 9,914
  • 7
  • 59
  • 102
  • You original version is fine. And this question is subjective and so not valid here. – David Heffernan Jun 20 '16 at 17:51
  • Read the [help]. This is a classic primarily opinion based close candidate. – David Heffernan Jun 20 '16 at 18:09
  • 1
    You can avoid loops at all using old good `goto` operator by the way ;o) – Abelisto Jun 20 '16 at 19:56
  • In my opinion the `while True` variant is better. You prefer different variants. Which one of us is objectively right? Neither, because it comes down to judgement and opinion. Hence this question should be closed as being "primarily opinion based". – David Heffernan Jun 21 '16 at 07:55
  • That's off topic also. That's a [list](http://meta.stackexchange.com/questions/98334/list-questions-community-wiki) question. Please delete this question. And then use the `while True` variant which is the idiomatic way to write this code. You are fighting against the lanaguage rather than trying to fit in with it. Don't swim against the tide. – David Heffernan Jun 21 '16 at 08:19

4 Answers4

2

Easy,

  function dotemp(a,b,c:integer;var value : integer):boolean;
  begin
    value:a*b+c;
    result:=value>0; 
  end;

  begin
    while dotemp(a,b,c,temp) do
        begin
           Operateontemp(temp);
         end;
  end;

Pascal has no lvalue assignments, but it DOES have var parameters.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
2

I don't quite see the problem. Why don't you invert the logic and forget about Break:

repeat
  Temp := MyFunction(1, 2, 3);
  if Temp <= 0 then
  begin
    //code which uses Temp, for example:
    ShowMessage(IntToStr(Temp));
  end;
until Temp > 0;

That does exactly the same as you first sample, i.e. the while True do code snippet, but neither calls the function twice, nor does it need to use an assignment as an expression (not possible in Delphi), nor does it need to exit early with Break.

In C, you would do this with

do
{
    ...
} while (temp <= 0);

Note that every

if (condition) then 
  Break;

can be replaced by

if not (condition) then
  // rest of original loop.
end;

for the rest of the loop.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • 1
    There's duplication. The test of Temp is performed twice. Better to store result in a local book or use while True and break, a respectable idiom. – David Heffernan Jun 21 '16 at 22:13
  • I don't think that using Break is better. And testing a variable twice is no big deal. – Rudy Velthuis Jun 22 '16 at 06:22
  • 1
    It's a violation of DRY as written in your answer. Which is what can be avoided by the two methods I offered. Still, I don't understand why we are answering opinion based questions. They should be closed. Did you vote to close? – David Heffernan Jun 22 '16 at 06:59
0

There is the possibility to use an infinite loop and Break:

while True do
  begin
    Temp := MyFunction(1, 2, 3);
    if Temp <= 0 then Break;
    ShowMessage(IntToStr(Temp));
  end;
finefoot
  • 9,914
  • 7
  • 59
  • 102
-1
Temp := 1;
while Temp > 0 do
begin
   //...
end;

Or repeat-until

JYass
  • 101
  • 1