From 4927091446942317273
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!newsfeed.stanford.edu!news.kjsl.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: Hyman Rosen <hyrosen@mail.com>
Newsgroups: comp.std.c++
Subject: Re: for-else: adding else to for loops
Date: Wed, 12 Aug 2009 13:49:39 CST
Organization: unknown
Lines: 38
Sender: cppmods@cs.rpi.edu
Approved: austern@google.com
Message-ID: <49Dgm.141164$Qg6.41767@newsfe14.iad>
References: <5d82cd6c-2a22-4dd7-8c7d-790b579533ea@d15g2000prc.googlegroups.com>
	<memo.20090807224336.1608A@brangdon.cix.compulink.co.uk>
	<84af1815-994d-48d6-9c07-c8965bba0d01@b25g2000prb.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Wed, 12 Aug 2009 13:41:19 -0400
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:1274

Jared Grubb wrote:

    I just thought I'd ask. Once you've seen the flow, it's hard to get
    out of your head when a use for it pops up!


How about this variant?

   for (auto i = c.begin(); i != c.end(); ++i)
       if (condition(*i))
           break;
   and
       // do this only if loop exited normally
       cout << "Condition never matched" << endl;
   or
       // do this only if loop never executed
       cout << "Empty container" << endl;

For an empty container, the above code would execute both the
'and' part and the 'or' part, in the order they appear, but
either part can have a 'break' statement to terminate further
processing. The scope of any variables declared by the 'for'
initialization expression extends through the 'and' and 'or'
clauses.

This syntax avoids any new keywords and is backwards-compatible.
It applies equally to while loops (the 'or' statement is never
executed for a do-while loop, naturally). And it has the same
infelicitous bind-to-nearest behavior that 'else' does :-) If
you want, you could require compound-statements for the 'and'
and 'or' parts, as 'try' and 'catch' do.

-- 
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



