Thread overview
Another Idea to Ponder: 2nd time blocks in loops
Nov 19, 2001
Russ Lewis
Nov 19, 2001
Pavel Minayev
Nov 19, 2001
Russ Lewis
Nov 20, 2001
Sean L. Palmer
November 19, 2001
Here's another ponder from the random files of Russ:



consider this code:

ReadInput();
while(!InputIsValid())
{
    PrintError();
    ReadInput();
};



This is fairly common code in a lot of my programs.  Ofc, instead of ReadInput(), I find that I have a line or three of code.  It would be advantageous to be able to avoid the duplication of the ReadInput() call.  Sometimes I do this by turning it into a false infinite loop:


while(1)
{
    ReadInput();
    if(InputIsValid())
      break;
    else
       PrintError();
};


But that's hard to read.  It would be nice to be able to add a block of loop-only code.  I don't have a good syntax, here's just a ponder:

do
{
    ReadInput();
} while(!InputIsValid())
loop
{
    PrintError();
};

Thoughts?


--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]


November 19, 2001
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BF92FF6.523E98C3@deming-os.org...

> ReadInput();
> while(!InputIsValid())
> {
>     PrintError();
>     ReadInput();
> };
>
> This is fairly common code in a lot of my programs.  Ofc, instead of ReadInput(), I find that I have a line or three of code.  It would be advantageous to be able to avoid the duplication of the ReadInput()

What about this:

    while(ReadInput(), !InputIsValid())
        PrintError();



November 19, 2001
Right, that's possible.  But, IMHO, that's even less readable than the infinite-loop thing.  Besides, I'm using ReadInput() for example clarity...in reality it's usually something far more complex, probably multiple lines.



>     while(ReadInput(), !InputIsValid())
>         PrintError();

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]


November 20, 2001
How about this syntax:

do
{
  ReadInput();
} if (!InputIsValid())
{
  PrintError();
}

or maybe this:

while (
    {
        ReadInput();
        ....
    },
    !InputIsValid()
    )
{
  PrintError();
}

or even this:

while
{
    ReadInput();
    ....
}
( !InputIsValid() )
{
  PrintError();
}

Wierd.  Would be a nice handy control structure though.

Sean

"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BF92FF6.523E98C3@deming-os.org...
> Here's another ponder from the random files of Russ:
>
>
>
> consider this code:
>
> ReadInput();
> while(!InputIsValid())
> {
>     PrintError();
>     ReadInput();
> };
>
>
>
> This is fairly common code in a lot of my programs.  Ofc, instead of ReadInput(), I find that I have a line or three of code.  It would be advantageous to be able to avoid the duplication of the ReadInput() call.  Sometimes I do this by turning it into a false infinite loop:
>
>
> while(1)
> {
>     ReadInput();
>     if(InputIsValid())
>       break;
>     else
>        PrintError();
> };
>
>
> But that's hard to read.  It would be nice to be able to add a block of loop-only code.  I don't have a good syntax, here's just a ponder:
>
> do
> {
>     ReadInput();
> } while(!InputIsValid())
> loop
> {
>     PrintError();
> };
>
> Thoughts?
>
>
> --
> The Villagers are Online! villagersonline.com
>
> .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
> .[ (a version.of(English).(precise.more)) is(possible) ]
> ?[ you want.to(help(develop(it))) ]
>
>