Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 19, 2001 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))) ] |
November 19, 2001 Re: Another Idea to Ponder: 2nd time blocks in loops | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "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 Re: Another Idea to Ponder: 2nd time blocks in loops | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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 Re: Another Idea to Ponder: 2nd time blocks in loops | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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))) ] > > |
Copyright © 1999-2021 by the D Language Foundation