From 3603585680348889783
X-Google-Thread: 7894ca11fe,d47b345505a72a70
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news1.google.com!news.glorb.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: Daphne Pfister <aw9jxzk02@sneakemail.com>
Newsgroups: comp.std.c++
Subject: Re: for-else: adding else to for loops
Date: Sat, 22 Aug 2009 12:37:18 CST
Organization: unknown
Lines: 136
Sender: cppmods@cs.rpi.edu
Approved: stephen.clamage@sun.com
Message-ID: <2009082201262116807-aw9jxzk02@sneakemailcom>
References: <5d82cd6c-2a22-4dd7-8c7d-790b579533ea@d15g2000prc.googlegroups.com>
  <9e9c2ca5-78eb-460c-adec-2ec051c54210@r18g2000yqd.googlegroups.com>
  <E1ihm.95676$BP6.35009@newsfe24.iad>
  <b3293cf8-8b06-4660-9581-50cadc471a3b@a13g2000yqc.googlegroups.com>
  <dqXhm.138643$BP6.80666@newsfe24.iad>
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: Sat, 22 Aug 2009 01:26:21 -0400
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:1315

On 2009-08-17 09:18:48 -0400, Hyman Rosen <hyrosen@mail.com> said:

> A language construct has a defined meaning. Why would a
> reader be in the dark about it? My suggestion to augment
> loops was
>       for (init; test; next) SL or SO and SA
>       while (test) SL or SO and SA
>       do SL while(test); and SA
> where a missing 'or' or 'and' part is taken to be there
> with a null statement. SO is executed if 'test' is false
> the first time it is evaluated. SA is executed if 'test'
> is ever false when evaluated. A 'break' in SL, SO, or SA
> moves control past the entire construct. Variables in the
> 'init' statement are in scope in SO and SA. Because of the
> semantics of the 'or' and 'and' parts, execution of SO
> falls into execution of SA, which is a nice touch.

Sounds almost like a switch... why not something like?

switch while (test)
{
case 1:
   // some code
   break; // exit the loop

case 2:
   // some code
   // fall though to next case

case 3:
   // some code
   continue; // go back to the top of the loop

default:
   // some code
   // fall through, and since last case would exit loop
}


Likewise with for:

switch for( init; test; next ) SL

Not only do they allow for something akin to original
suggestion they can simplify some scanning loop flows.
In particular it eliminates having to worry about adding
a done bool to handle a common case of having a switch
directly inside a for/while loop.

For
example:

// Skip whitespace but keep track of line numbers
char* c_str_ptr = ...;
int line_num = 0;
switch for ( ; *c_str_ptr; ++c_str_ptr )
{
   case '\n':
     ++line_num;
   case ' ':
   case '\t':
     continue;

   case 0:
     // end of string reached before non-white space
     // do something fancy if desired
     break;

   // This default does not really need to be here.
   // For exhibition only
   default:
     // non-white found
     break;
}

Combine with support for "case if (test):" and you have even
more power.

switch for ( auto my_iter = x.begin(); *my_iter; ++my_iter )
{
   case if ( my_iter == x.end() ):
     // Note *my_iter would not have been evaluated yet
     // since only cases seen so far are case ifs.
     std::cout << "Needle not found" << std::endl;
     break;

   case NEEDLE1:
     std::cout << "Needle1 found" << std::endl;
     break;

   case NEEDLE2:
     std::cout << "Needle2 found" << std::endl;
     break;

   default:
     continue;
}

My thoughts on case if would be that the switch test would
not be evaluated until the first case constant: or default:
(including the implicit default: at the bottom of a switch
if not present) was seen. And any case if would only be
evaluated if none of the cases before matched. Compilers
would still be able to take emit jump tables, but would have
to split them for crossing an "case if" boundary.

This would mean that the form

switch for( auto i : seq )
{
   // ...
}

Could be the same as

switch for( auto i = begin( seq ); *i; ++i )
{
   case if ( i == end( seq ) ):
     break;

   // ...
}

I'm aware that this suggestion does not completely cover the
"or SO" case of Hyman Rosen's suggestion.

Daphne Pfister



-- 
[ 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                      ]



