Board index » delphi » New Delphi/Object Pascal language feature proposal

New Delphi/Object Pascal language feature proposal


2006-11-24 01:14:27 AM
delphi65
I'll took this from the "Poll: Using the Goto Delphi statement?" thread...
Quote
How about you? Are you using the Goto in your Delphi code?
Yes, I had a special case not expected by regular structures like "if", "break" or "while": a "repeat-while" statement.
Just suppouse there is a combination of the "repeat-until" & the "while-do" structures. The first section is always executed, unleast once, as it happens with the "repeat-until" flow structure, and the second section may not be executed at all, as it happens with the "while-do" flow structure.
Example:
---------------
...
repeat
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
while (uppercase(Answer) <>'EXIT') do
begin
FirstName := Answer;
Write('Give me your last name');
ReadLn(LastName);
...
end;
OtherStuff;
// If condition in "while" is "true",
// the block is executed, and control goes back to "repeat",
// not to "OtherStuff"
// When condition in "while" fails,
// then goes to "OtherStuff"
...
---------------
Since, this structure doesn't exists, "goto" statements come to the rescue:
---------------
label repeat_label, skip_label;
...
repeat_label:
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
if (uppercase(Answer) <>'EXIT') then
begin
FirstName := Answer;
Write('Give me your last name');
ReadLn(LastName);
...
end else goto skip_label;
goto repeat_label;
skip_label:
OtherStuff;
...
---------------
Some people will prefer a non "goto" alternative:
---------------
function CanContinue(var Answer: string): boolean;
begin
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
Result := (uppercase(Answer) <>'EXIT');
end;
...
while (CanContinue(Answer)) do
begin
FirstName := Answer;
Write('Give me your last name');
ReadLn(LastName);
...
end;
OtherStuff;
...
---------------
Other will use an "if" instead:
---------------
...
var CanContinue: Boolean;
...
repeat
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
CanContinue := uppercase(Answer) <>'EXIT';
if (CanContinue) then
begin
FirstName := Answer;
Write('Give me your last name');
ReadLn(LastName);
...
end;
until (not CanContinue);
OtherStuff;
...
---------------
Another new Delphi/Object Pascal language feature proposal ("repeat-until" or "repeat-loop") ?
Just my 2 cents...
maramirezc
 
 

Re:New Delphi/Object Pascal language feature proposal

"42" <XXXX@XXXXX.COM>writes:
Quote
OtherStuff;

or did I miss something?
No, you didn't. I actually post the "if" alternative in other post. The point here, with the new "repeat-while-do" structure, code will have more logical meaning.
It's like you told me: "why we don't use ifs and remove case from pascal"
----------------
if (c='a') then ...
else if (c='b') then ...
else if (c='d') then ...
else ...
----------------
instead of
----------------
case (c) of
'a': ...
'b': ...
'd': ...
else ...
----------------
which it is more clear to the programmer.
Just my 2 cents...
 

Re:New Delphi/Object Pascal language feature proposal

"maramirezc" <XXXX@XXXXX.COM>writes news:4565e503$XXXX@XXXXX.COM...
Quote

I'll took this from the "Poll: Using the Goto Delphi statement?" thread...

>How about you? Are you using the Goto in your Delphi code?

Yes, I had a special case not expected by regular structures like "if", "break" or "while": a "repeat-while" statement.

Just suppouse there is a combination of the "repeat-until" & the "while-do" structures. The first section is always executed, unleast once, as it happens with the "repeat-until" flow structure, and the second section may not be executed at all, as it happens with the "while-do" flow structure.

Example:

---------------
...

repeat
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
while (uppercase(Answer) <>'EXIT') do
begin
FirstName := Answer;

Write('Give me your last name');
ReadLn(LastName);

...
end;

OtherStuff;
Why not
while True do
begin
Write('Give me your first name, or "exit" to finish:');
ReadLn(Answer);
if uppercase(Answer) = 'EXIT' then
Break;
Write('Give me your last name');
ReadLn(LastName);
end;
OtherStuff;
or did I miss something?
 

Re:New Delphi/Object Pascal language feature proposal

Quote
Since, this structure doesn't exists, "goto" statements come to the
rescue:
Definetly "goto" does not come to mind at all, in this case. I can do a
lot better without it. In cases where repeat-until is embeeded with
while-do loops I'd rather use small procedures or functions with a
very clear name, for each block in the loop. I only use "label" in code
like this
while (Index < FCount) do
begin
repeat
Item := FList[Index];
asm
... assembler {*word*99}
OR AL,AL
JNE RETURN
end;
Inc(Index);
until (Index = FCount);
RETURN:
But than again, I looked at some old code (8yrs) of mine
and found this:
TRYAGAIN:
... some computation
if SomeCondition then
begin
... more computation
if Condition1 then goto TRYAGAIN
else goto EXITBLOCK;
end;
... some more computation
EXITBLOCK:
I guess in '98 I couldn't come up with something better :-(
Currently I am staying pretty much away of gotos,
at least I am trying
Lucian