Topic: for loops


Author: maxtal@extro.ucc.su.OZ.AU (John (MAX) Skaller)
Date: Tue, 7 Jul 1992 05:00:19 GMT
Raw View
While on the subject of 'for' loops and their control variables,
since 'for' loops in C are a bit broken anyhow, if the control
variable was made local to the loop body it would complicate
a 'search' loop.

 for(int i=0; i<10; ++i)if(a[i]==x)break;
 if(i==10){not found}
 else{found at i}

This is nasty: typical loops either run their full term
or are prematurely broken. The two cases are usually handled
differently.

 How would you code the above if the control variable was local?

 What idioms do people use for the above case?

 Anyone think exceptions are appropriate?

 I saw an article by Knuth showing a new control
 structure that allowed 'break name' in effect,
 and the named bits were attached at the end
 of the loop but considered in the same scope.

 What is a better control structure to handle this case
and similar ones?




--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------