|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - Another Idea to Ponder: 2nd time blocks in loops
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
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3BF92FF6.523E98C3 deming-os.org... Nov 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. Nov 19 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...
Nov 20 2001
|