Topic: Updated for loop syntax
Author: "VAN NATTER, DOUG C, ATTSI" <dv8285@att.com>
Date: Tue, 30 Jun 2009 09:33:13 CST Raw View
I frequently find myself writing code that does a linier search through some
array, something like this:
int i, n;
struct {int tag; char *value} X[n];
for (i = 0; i < n; i==) {
if(X[i].tag == tag) { // found it
break;
}
}
if(i == n) {
// didn't find it
Do something
}
It would be very convenient if the "if" didn't need to be there. Its purpose
isn't always clear, the condition may need to be updated if the "for"
condition is updated, it may cause the compiler to generate more code than
necessary when the condition is not trivial. Python allows a syntax like
this:
int i, n;
struct {int tag; char *value} X[n];
for (i = 0; i < n; i==) {
if(X[i].tag == tag) { // found it
break;
}
}
else {
// didn't find it
Do something
}
The "break" breaks past the "else" clause. The else clause is processed if
and only if the for loop completes, so there is no need to reevaluate the
condition. Using a standard syntax makes the purpose clear. And, there is
nothing else to update if the "if" condition ever changes. I'm not real
certain that "else" is the most appropriate keyword (perhaps "finally" or
"when done" would be better).
This change seems unlikely to break any existing code, makes it easier for
the compiler to generate efficient code, makes the developers intent more
clear, and makes maintenance changes to existing code more likely to be done
correctly.
Doug Van Natter
MSS-Network Based (Gemini)
AT&T Chief Security Office (CSO)
205 403-2640
--
[ 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 ]
Author: CornedBee <wasti.redl@gmx.net>
Date: Tue, 30 Jun 2009 16:58:37 CST Raw View
On Jun 30, 5:33 pm, "VAN NATTER, DOUG C, ATTSI" <dv8...@att.com>
wrote:
>
> int i, n;
> struct {int tag; char *value} X[n];
> for (i = 0; i < n; i==) {
> if(X[i].tag == tag) { // found it
> break;
> }}
>
> else {
> // didn't find it
> Do something
>
> }
>
> This change seems unlikely to break any existing code,
It would break code like this:
if(condition)
for(for-clause) {
; // for-body
}
else
; // else-statement
The else would suddenly and silently bind to the for loop instead of
the if.
Sebastian
--
[ 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 ]
Author: Mathias Gaunard <loufoque@gmail.com>
Date: Tue, 30 Jun 2009 16:59:53 CST Raw View
On 30 juin, 17:33, "VAN NATTER, DOUG C, ATTSI" <dv8...@att.com> wrote:
> I frequently find myself writing code that does a linier search through some
> array, something like this:
>
> int i, n;
> struct {int tag; char *value} X[n];
> for (i = 0; i < n; i==) {
> if(X[i].tag == tag) { // found it
> break;
> }}
>
> if(i == n) {
> // didn't find it
> Do something
>
> }
> It would be very convenient if the "if" didn't need to be there. Its purpose
> isn't always clear, the condition may need to be updated if the "for"
> condition is updated, [...]
> This change seems unlikely to break any existing code, makes it easier for
> the compiler to generate efficient code, makes the developers intent more
> clear, and makes maintenance changes to existing code more likely to be done
> correctly.
What about writing the code as
X* p = find_if(X, <&>(mystruct s) { return s.tag == tag; });
if(p != X+n)
{
// found it
}
else
{
// didn't find it
}
That makes it even more idiomatic, maintainable and explicit.
--
[ 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 ]
Author: JJ <jjnoakes@gmail.com>
Date: Tue, 30 Jun 2009 17:00:46 CST Raw View
On Jun 30, 11:33 am, "VAN NATTER, DOUG C, ATTSI" <dv8...@att.com>
wrote:
> Python allows a syntax like this:
>
> int i, n;
> struct {int tag; char *value} X[n];
> for (i = 0; i < n; i==) {
> if(X[i].tag == tag) { // found it
> break;
> }}
>
> else {
> // didn't find it
> Do something
>
> }
>
> The "break" breaks past the "else" clause. The else clause is processed if
> and only if the for loop completes, so there is no need to reevaluate the
> condition. Using a standard syntax makes the purpose clear. And, there is
> nothing else to update if the "if" condition ever changes. I'm not real
> certain that "else" is the most appropriate keyword (perhaps "finally" or
> "when done" would be better).
>
> This change seems unlikely to break any existing code
If you use the 'else' keyword, you may break existing code:
if (cond)
for (...) {
body;
}
else {
body;
}
-JJ
--
[ 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 ]