🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2503

Re: New treatment of 'for (int i ...'

#2503
From: Dr. Richard Botting
Date: Fri, 10 Nov 1995 05:21
94 lines
4013 bytes
Steven Correll (sjc@netcom.com) wrote:
: What was the rationale for this change, which from here looks quite
: gratuitous? It's reminiscent of the old Pascal rule that the value of a
: loop index was undefined after the loop exited; 

>From within the C mindset it looks counter-intuitive.

It is effectively a return to the FOR loop of Algol 68 and
and a certain language standardized in 1983.  The 1968 form
reflected the informal use bound variables in mathematics
and their use in quantifiers (for all x...) in symbolic logic
and the lambda calculus.

The 1983 motivation was most un-C-like:  It was thought 
that it should lead to fewer bugs.  Experience shows that it is an
occasional pain in the neck, but does eliminate a set of surprises.
Indeed I had a kind of example of this effect, while teaching the
basics of for-loops.  I had a laboratory where the students had to
find my error in the following code
	int i;
	int sum=0
	for( i=1; i<=n; i++);
		sum := i;
	cout<<sum; //produces n+1... why?
Now the tightly bound loop on a standard compiler
	int sum=0;
	for(int i=1; i<=n; i++);
		sum +=i;
	cout <<sum;
will not compile.

Unlike most safety mechanisms it is not difficult to
work around.  However a wise programmer will use a more sophisticated
rule than:
	Make scopes as small as possible.
Perhaps something like:
	Make the scope meaningful
is a better rule.

For example, suppose we have a string of characters and want to
scan across it from start to finish, but treat it as a sequence of
three different iterations then we would write
	{//Start of scan
		char *pointer=string;
		for(; whatever(pointer); pointer++)
			dowhatever(pointer);
		for(; whatever2(pointer); pointer++)
			dowhatever2(pointer);
		for(; whatever3(pointer); pointer++)
			dowhatever3(pointer);
	}//end of scan
because the meaning of pointer is the same - even as its value
changes - throughout the block and across 3 for-loops.

However if we want to do three scans of the same string, one after
another:
		for(char *q=string; whatever(q); q++)
			dowhatever(q);
		for(char *q=string; whatever2(q); q++)
			dowhatever2(q);
		for(char *q=string; whatever3(q); q++)
			dowhatever3(q);
There are clear opportunities for a compiler to optimize
loops like this - because the scopes do not overlap.

One can imagine a combination.... A long scan of the whole
string while we process each part together with three different strings:
		char *p=string;
		for(char*q=start1; whatever(p,q); p++,q++)
			dowhatever(p,q);
		for(char*q=start2; whatever2(p,q); p++,q++)
			dowhatever2(p,q);
		for(char*q=start3; whatever3(p,q); p++,q++)
			dowhatever3(p,q);
This extends to counters, subscripts, indexes, and iterators.

This discipline breaks down, of course, when the meaningful
life of two or more variables do not nest but overlap.  This
is a kind of logical parallelism that is as hard to believe in
as it is to code. (and also off topic!)

--
	dick@csci.csusb.edu=rbotting@wiley.csusb.edu.
Find out what's new at http://www.csci.csusb.edu/doc/www.sites.html
Disclaimer:`CSUSB may or may not agree with this message`.
Copyright(1995):Copy this freely but include the following link to the
<a href="http://www.csci.csusb.edu/dick/signature.html">author's signature</a>


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <47u9tv$9g7@news.csus.edu>
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt
References: <47itjt$65m@lhc.nlm.nih.gov> <sjcDHqMKM.MBG@netcom.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt
From: dick@silicon.csci.csusb.edu (Dr. Richard Botting)
Newsgroups: comp.std.c++
Subject: Re: New treatment of 'for (int i ...'
Date: 10 Nov 1995 05:21:59 GMT
Organization: CS Dept., Calif. State Univ., San Bernardino
Approved: Matt@Austern
Message-ID: <47u9tv$9g7@news.csus.edu>
References: <47itjt$65m@lhc.nlm.nih.gov> <sjcDHqMKM.MBG@netcom.com>
NNTP-Posting-Host: austern.hip.berkeley.edu