473,221 Members | 2,199 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Exiting a loop half way through

While playing around with my quine I was reminded
of a situation I have come across before: when
the exit-condition for a loop needs to be checked
in the middle of a loop, rather than at the
beginning (while) or the end (do ... while). It
always seems to come up when I'm hand-rolling very
simple parsers. (Doctor, it hurts when I do this ...)

Anyone want to argue for or against any of these
three alternatives, or suggest something better?

template <typename RulesFuction>
void format (const string & s, RulesFunction f)
{
// f will be applied to an iterator to a '%' character.
// Applying f should return an iterator to the character
// after the whole '%' escape sequence.
// Side-effects of f should be limited to output on cout.

iter b (s.begin ()), e (s.end ()), i;
ostream_iterator <char> out (cout);

#if defined A

copy (b, i = find (b, e, '%'), out);
while (i != e) {
b = f (i);
copy (b, i = find (b, e, '%'), out);
}

#elif defined B

while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);
}

#else

goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);
}

#endif

}

Jul 19 '05 #1
5 3850
Buster Copley wrote:
While playing around with my quine


Practice what you preach, Buster. The complete,
compilable code follows. You can see which way
I've settled on at the moment.

#include <iostream>
#include <ostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
typedef string::const_iterator iter;

template <typename F>
void format (const string & s, F f) {
iter b (s.begin ()), e (s.end ()), i;
ostream_iterator <char> out (cout);
goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);
}
}

iter p (iter i) {
cout << '%' << * ++ i;
if (* i == 'n') cout << "\"\n \"";
return ++ i;
}

struct q {
iter operator () (iter i) {
switch (* ++ i) {
case 's': format (s, p); break;
case 'n': cout << '\n'; break;
case 'q': cout << '\"'; break;
case 'b': cout << '\\'; break;
default: cout << * i;
}
return ++ i;
}
q (const string & s) : s (s) { }
const string & s;
};

int main () {
string s (
"#include <iostream>%n"
"#include <ostream>%n"
"#include <string>%n"
"#include <algorithm>%n"
"#include <iterator>%n"
"using namespace std;%n"
"typedef string::const_iterator iter;%n"
"%n"
"template <typename F>%n"
"void format (const string & s, F f) {%n"
" iter b (s.begin ()), e (s.end ()), i;%n"
" ostream_iterator <char> out (cout);%n"
" goto start;%n"
" while (i != e) {%n"
" b = f (i);%n"
" start: copy (b, i = find (b, e, '%%'), out);%n"
" }%n"
"}%n"
"%n"
"iter p (iter i) {%n"
" cout << '%%' << * ++ i;%n"
" if (* i == 'n') cout << %q%b%q%bn %b%q%q;%n"
" return ++ i;%n"
"}%n"
"%n"
"struct q {%n"
" iter operator () (iter i) {%n"
" switch (* ++ i) {%n"
" case 's': format (s, p); break;%n"
" case 'n': cout << '%bn'; break;%n"
" case 'q': cout << '%b%q'; break;%n"
" case 'b': cout << '%b%b'; break;%n"
" default: cout << * i;%n"
" }%n"
" return ++ i;%n"
" }%n"
" q (const string & s) : s (s) { }%n"
" const string & s;%n"
"};%n"
"%n"
"int main () {%n"
" string s (%n"
" %q%s%q);%n"
" format (s, q (s));%n"
"}%n"
"");
format (s, q (s));
}

Jul 19 '05 #2
Buster Copley wrote:
While playing around with my quine I was reminded
of a situation I have come across before: when
the exit-condition for a loop needs to be checked
in the middle of a loop, rather than at the
beginning (while) or the end (do ... while). It
always seems to come up when I'm hand-rolling very
simple parsers. (Doctor, it hurts when I do this ...)

Anyone want to argue for or against any of these
three alternatives, or suggest something better?

copy (b, i = find (b, e, '%'), out);
while (i != e) {
b = f (i);
copy (b, i = find (b, e, '%'), out);
Code duplication.
#elif defined B

while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);
How I used to do that (if I had to) was to add // NOTE! after the break.
So no one will miss it.
goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);


Ahh. I looked at a goto. Now I have to go and confess. :-)

I sort of like that. It reminds me of the Duff's device and this:

http://www.cuj.com/documents/s=8890/cujexp0310dewhurst/

Since I am coming down with a fever (and it is not Saturday night here) I
might have missed important things here.

--
WW aka Attila
Jul 19 '05 #3
White Wolf wrote:
copy (b, i = find (b, e, '%'), out);
while (i != e) {
b = f (i);
copy (b, i = find (b, e, '%'), out);
Code duplication.


Yeah, that's my least favourite.
#elif defined B

while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);

How I used to do that (if I had to) was to add // NOTE! after the break.
So no one will miss it.


Good call.
goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);

Ahh. I looked at a goto. Now I have to go and confess. :-)

I sort of like that.


Me too.
It reminds me of the Duff's device and this:

http://www.cuj.com/documents/s=8890/cujexp0310dewhurst/

Since I am coming down with a fever (and it is not Saturday night here) I
might have missed important things here.


Thanks a lot for your comments.

Jul 19 '05 #4
"White Wolf" <wo***@freemail.hu> wrote in message
news:bk**********@phys-news1.kolumbus.fi...
#elif defined B

while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);


How I used to do that (if I had to) was to add // NOTE! after the break.
So no one will miss it.


Hello, Wolf and Buster.

Did you ever consider:

while(true)
{
doSomethingFirst();
if (condition)
break;

doSomethingElse();
}

or possibly even (although I wouldn't do this):

while(true)
{
doSomethingFirst();

if (condition) break;

doSomethingElse();
}

To me whitespace seems the most natural way to emphasize control statements,
since most whitespace is already used for that very purpose.

Jul 19 '05 #5
"Kevin Saff" <go********@kevin.saff.net> wrote
while(true)
{
doSomethingFirst();
if (condition)
break;

doSomethingElse();
}

To me whitespace seems the most natural way to emphasize control statements,
since most whitespace is already used for that very purpose.


Thanks very much for your response.
Buster
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: vineoff | last post by:
If I'm having nested loops like: for (...) for (..) for (...) { /* exit here */ } and I need to exit from there ^ . Is it better to use exceptions or goto or some other method?
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
2
by: pnp | last post by:
Hi all, I'm working on a win app that uses a logon form to verify the user that logs in the program and then hides the first form and displays an MDI form where the user does his work. The app is...
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
6
by: John (Z R) L | last post by:
Hi all, I am very new to programming, and I chose to study the Python language before C++. I am currently using the Wikibooks "Non-Programmer's Tutorial for Python", and am up to the section "Who...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
5
by: dp_pearce | last post by:
I have some code that takes data from an Access database and processes it into text files for another application. At the moment, I am using a number of loops that are pretty slow. I am not a...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.