www.digitalmars.com         C & C++   DMDScript  

D - Another Idea to Ponder: 2nd time blocks in loops

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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))) ]
Nov 19 2001
next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"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();
Nov 19 2001
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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))) ]
Nov 19 2001
prev sibling parent "Sean L. Palmer" <spalmer iname.com> writes:
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))) ]

Nov 20 2001