are for loops and while loops the same thing?

Gregory Smith greg at utcsri.UUCP
Tue Apr 8 03:36:23 AEST 1986


In article <139 at mit-amt.MIT.EDU> judith at mit-amt.MIT.EDU (Judith Donath) writes:
[example deleted. I have substituted this from K&R pg. 203]
	while(...){			for(...){
	    ...				    ...
	    contin:;			    contin:;
	}				}
[ Judith pointed out that 'continue' does the re-initialization step on the
for loop ]
>
>Is there any reason or use for this difference in behaviour between 
>while and for?  

Definitely. The re-initialization step is intended to be part of the loop
control. 'continue' is intended to mean 'abandon this execution of the loop
and go on to the next'. Thus you would want the re-initialization step done,
so the loop control will work properly.
E.g.: execute loop for i==1 to 10, but not for i == skip:

	for(i=1; i<=10; ++i ){
		if( i==skip) continue;
		...
	}
If ++i was not done after continue, an infinite loop would result.

>Are there any implementations in which a continue 
>in a for loop passes control to the test, as if for was really
>the equivalent of the above structured while loop?

If there are, they are incorrect, and they are in trouble.
-- 
"If you aren't making any mistakes, you aren't doing anything".
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list