Re: Difference between for and while loop
On Friday 18 April 2003 13:46, Frank Gevaerts wrote:
> On Fri, Apr 18, 2003 at 12:58:43PM +0300, Aryan Ameri wrote:
> > On Friday 18 April 2003 03:52, tjm3 wrote:
> > > This is the way it was explained to me.
> > >
> > > A FOR loop is usually used to execute a routine or instruction(s)
> > > a fixed number of times, 'fixed' meaning that going into the
> > > loop, you already know how many iterations will be made. The FOR
> > > loop should also be used when you know that you will be executing
> > > the contents of the loop at least once.
> >
> > Well, the way it has been explained to me, you can also use the FOR
> > loop in non defined loops, and you can also set a FOR loop, in a
> > way that the minimum execution time might be less than 1, say 0.
>
> What language are we talking about ?
I was talking about C. I guess that was the OP's question.
> - sh : for loops loop a variable over a number of values ( i.e. for i
> in 1 2 3 ) and while loops loop until a condition is met
>
> - C : they are equivalent. Only the notation differs, and which one
> to use in a certain context is mostly a question of style and coding
> rules.
> The following is a perfectly valid use of for, but it is slightly
> unconventional
>
> int f;
> char buf[3];
> for(f=open("/tmp/file",O_RDONLY);read(f,buf,1)>0;)
> {
> }
> close(f);
Yup, I was refering to this, exactly.
> - pascal : for loops are for numbers and enumerated types, while
> loops use a condition
>
> - basic : somewhat like pascal
>
> - theoretical : both while and for loops are unneeded : recursion is
> much nicer
Newbish question, what's recursion ?
Cheers
--
/* Impeach God */
--RMS
Aryan Ameri
Reply to: