Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linearizeing nested IFs

11 views
Skip to first unread message

Anton Shepelev

unread,
Apr 4, 2011, 7:52:47 AM4/4/11
to
Hello all,

The standard Pascal (ISO 7185) doesn't have a
'return' statement, so a function or procedure will
execute to the end. I often encounter situations in
which multiple sequential checks must be done so
that:

a. the failure of one check interrupts the whole
sequence and

b. each seccessive check may use the result cal-
culated in the previous check.

This can be represented like many nested IF state-
ments, which is ugly when the nesting level is high.
Languages with a non-structured 'return' statement
allow to linearize this, but what to do in Pascal?

I thought about looping through an array of fucntion
pointers, but this seems too much of an overhead to
me...

Thank you in advance,
Anton

Marco van de Voort

unread,
Apr 4, 2011, 8:12:42 AM4/4/11
to
On 2011-04-04, Anton Shepelev <anto...@gmail.com> wrote:
> This can be represented like many nested IF state-
> ments, which is ugly when the nesting level is high.
> Languages with a non-structured 'return' statement
> allow to linearize this, but what to do in Pascal?
>
> I thought about looping through an array of fucntion
> pointers, but this seems too much of an overhead to
> me...

If you limit yourself to std pascal, afaik your options are:

1. GOTO to somewhere near the end of the procedure
2. GOTO directly to the parent procedure. (if nested procedure)

the second is mainly interesting if you want to execute special
errorhandling in the parent procedure.

Chris Burrows

unread,
Apr 4, 2011, 6:20:52 PM4/4/11
to
"Anton Shepelev" <anto...@gmail.com> wrote in message
news:20110404155247.5...@gmail.com...

Chuck Mowle, a former colleague of mine, once suggested an approach that
goes something like this:

(* untested! *)
checkNo := 0;
WHILE ok AND (checkNo < maxChecks) DO BEGIN
INC(checkNo);
CASE checkNo OF
1:
2: .....
...
...
END (* case *)
END (* while *)
IF NOT ok THEN Write("failed on check", checkNo);

Regards,
Chris Burrows

CFB Software
Astrobe v3.3: ARM Oberon-07 Development System
http://www.astrobe.com

Anton Shepelev

unread,
Apr 6, 2011, 5:04:23 AM4/6/11
to
Thank you, Marco and Chris.

Anton

stuart

unread,
Apr 13, 2011, 5:44:11 PM4/13/11
to

You don't actually have to nest the IF statements, you can have a
variable that indicates whether or not a check has failed and then
test this variable before each step in the sequence.

So for example instead of

if check1 then
begin
do something1
if check2 then
begin
do something2
if check3 then
begin
do something3
end
end
end
end

you could use

ok := check1;
if ok then
begin
do something1;
ok := check2;
end;

if ok then
begin
do something2;
ok := check3;
end;

if ok then
begin
do something3;
end;

I write code like this sometimes in C (which does have a return
statement) when I don't want to use the return because I have a
function which needs to do some cleanup before it exits.

Anton Shepelev

unread,
Apr 23, 2011, 3:39:07 PM4/23/11
to
Stuart:

> [...]


> if ok then
> begin
> do something2;
> ok := check3;
> end;
>
> if ok then
> begin
> do something3;
> end;

> [...]

Thank, this is another good idea.

Anton

0 new messages