Click here to Skip to main content
15,173,311 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I read that for loop is called counter controlled loop.because it has a variable to control the number of iterations.

my question is that why while loop is not called counter controlled loop?

Your premise may be true of BASIC-like languages (and Fortran), but it is not true of C-like languages. C and its offspring allow much more flexibility in how you control a for loop than simple iteration.
Tim Carmichael 28-Jun-13 10:28am
I agree with your statement; my comment was very generalized.
I tend to view 'for' loops as iterized, 'while' loops as 'as long as this condition is not met' and 'until' loops as 'as least once, and then as long as condition is not met'.
In that context, I use 'for' loops largely for indexing; 'while' loops for an indeterminite number of passes (for example: while a cell value is not blank in Excel), and I avoid 'until' loops as a rule.
A 'for' loop is counter controlled because the loop ends when a counter reaches its limit.
A 'while' loop end when a boolean condition is no longer met, which may be that a counter has exceeded a limit or something in the loop itself if checking the condition and allowing the loop to end.

Frequently, I write 'while' loops with a boolean variable; ex: while (bExit != TRUE)
Within the loop code, I may have various conditions that will allow bExit to be set to true.
H.Brydon 28-Jun-13 10:26am
... and you wouldn't release that to any code base of mine. Boolean values are expressions by themselves and don't need comparison with a value. The correct use of a bool/BOOL for this type of code is "while(!bExit)"
Tim Carmichael 28-Jun-13 13:15pm
The (bExit != TRUE) was for readability, not functionality. I'd be much more likely to write a TRUE test than a FALSE test.
For example: While(bStillGoing) as opposed to While(!bGetOut).

Not all that looks like code is code; it is quite often pseudocode for readability.
Not only what Tim said, but (and my CPP is a little rusty) you can do something like this:

struct {
  int val1;
  int val2;
} myStruct;

myStruct[] myStructArray;

//Do stuff that initializes the structure

for (int *ptr = &myStructArray; ptr <= &myStructArray[lastElem]; ptr+=sizeof(myStruct))
   //Manipulate each item in the structure

May not be compilable, but you get the point. For loops don't necessarily use "counters", they use iterators. You can do some pretty strange stuff with while loops:

for (bool flag = false;flag;)
    //keep doing something until you flip flag

for (;;)
    //Infinite loop

Etc... The for loop is just an initialization, condition, and a statement that runs every time it loops (which is typically used for iteration or counting.

So writing out what a "for" loop means:

for (initial condition) until (condition is true) do (for body) then (increment variable)

Which in its compact form, becomes:

for (initial condition; exit condition; increment variable)
 method body

So its not called "counter controlled loop" because its not necessarily controlled by a counter, Its still a condition check (strangely enough, compilers turn For loops into while loops, so you can do the same with while loops as for loops and vice versa).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900