Looping Construct - Thoughts?

snarfblam

Mega-Ultra Chicken
Joined
Jun 10, 2003
Messages
1,832
Location
USA
User Rank
*Expert*
Just hoping to get some input for a DotNet project Im working on, and figured that C# programmers would be the best to ask. If a simplified looping structure were introduced to C# (using the hypothetical "loop" keyword) I would like to hear thoughts on a few points. This hypothetical loop construct would be a simplified version of the for loop whose form can very depending on how specific the programmer would like to be. The form would be:

Code:
[COLOR="Blue"]loop[/COLOR](controlVariable, start, count) [COLOR="Magenta"][I]statement[/I][/COLOR];

The start value can be omitted (assumed to be zero), or the controlVariable and the start value can be omitted, so some possible variants would be:

Code:
[COLOR="SeaGreen"]// Curly braces arent really necessary.[/COLOR]
[COLOR="Blue"]loop[/COLOR](numberOfTimes) { 
    MessageBox.Show("You will see this a number of times.");
}

[COLOR="Blue"]loop[/COLOR]([COLOR="Blue"]int[/COLOR] i, numberOfTimes) {
    MessageBox.Show("You have seen this " + (i + 1).ToString() + " times.");
}

[COLOR="Green"]// This will loop from 5 to 14 (start at five, ten iterations)[/COLOR]
[COLOR="Blue"]loop[/COLOR]([COLOR="Blue"]int[/COLOR] i, 5, 10) 
    MessageBox.Show("I started at 5, and now Im at " + i.ToString());
The idea, of course, is to take the ugly c-style for loop and create something a little more elegant and something even more concise than the VB for loop. My biggest concern is the fact that with different forms of the loop the expressions appear in a different order (i.e. the control variable might be first, or the count might be, and the initial value might be second, or the count may be). Does this seem like it has potential for confusion? I could change the order around to eliminate this problem, but the order I have chosen seems most intuitive to me.

Another concern of mine is that some people have questioned whether the programmer should be specifying a number of iterations or a terminating value. For example, should loop(int i, 5, 10); loop from 5 to 10 (10 is the terminating value) or should it loop from 5 to 14 (10 is the number of iterations). I personally prefer the latter. Even though it is slightly less intuitive, it results in neater code (you avoid the "-1" that is ever present in VBs for loops).

If it makes any difference, this simplified loop would be used primarily in scripting rather than compiled code. Any thoughts are welcome.
 
My opinion: You cant get much simpler and more concise than a for loop for the examples above. Plus it avoids all of the confusion of your later discussion, does it loop 10 times or to 10? does it include the 10 or does it really stop at 9? and so on. With a for loop, the code does exactly what you tell it to, no confusion or ambiguity.

Code:
for (int i = 0; i < numberOfTimes; i++) { ... } //This Should cover cases 1 and 2. Is the control variable even necessary in your proposed solution?

for (int i = 5; i < 10; i++) { .. }


Of course, if you dont like the way that looks then it really doesnt matter much how diverse yet simple regular for loops are.

If you want to add something unique I think the loop a certain number of times idea is a good one.

Code:
loop (10) { ... } //loops 10 times doesnt matter if its 0-9 or 1-10
loop (list.count) { ... } // loops the number of items in the list, no worry about 0 based or 1 based lists. This could also be a poor mans iterator.
loop (10, 5) { ... } //loop 10 times starting at 5 (goes to 15)

 
Thanks for the input. Im sure plenty of people appreciate the... explicitness... of the C-style for loop. As for simpler or more concise, I would disagree. I think it is rather redundant. It is almost always seen in a very standard form: declaring a control variable, initializing it, a less than comparison with a termination value, and an increment operation (dont forget the loops contents). Any deviation tends to be viewed as bad style because it becomes convoluted (and a simpler while loop becomes more appropriate), yet we are still expected to write out every last fundamental bit of the logic with the exception of the actual GOTO command. The C-style for loop is nothing more, really, than some syntactic sugar that glues together several statements, and, frankly, it leaves a sour taste in my mouth.

I see no need to have to write out any more of the loop than what actually ever changes from one for loop to the next: the control variable and the limits of the loop. And if one can be omitted, then why not?

That being said, I dont think your opinion is invalid. I simply dont share your opinion, but I will take it into consideration. I do know that others have complained about how the C-style loop is a little to involved, specifically users of VB (the language often criticized for being overly verbose).

I dont know if the idea is a little sillier or less relevant that I thought, or if maybe these boards have become too quiet to be a worthwhile place to get some input, but there certainly seems to be a lack of interest in a simplified for loop here, which strengthens your case, I suppose, for the superfluousness of said loop, which I will also take into consideration. I do remember asking a very similar question a while back and getting a much bigger response...
 
It is almost always seen in a very standard form: declaring a control variable, initializing it, a less than comparison with a termination value, and an increment operation
That is basically true. There is another variation often seen in C++ that would likely be quite foreign to pure .Net or Java coders. As Ive learned since college, this is more an academic exercise but code for traversing a hand rolled linked list will be slightly different (though you could do it with pointer arithmetic and in that way keep the incrementor. That is definitely frowned upon these days for some reason ;))

My C++ is a little rusty, but it would go a little something like this:
Code:
struct node
{
   int someData;
   node* next;
};

 // define a linked list of nodes...

for (node* current = head; current != 0; current = current->next) { ... }

In this case, the for loop is a fairly tight way to make sure you dont forget anything. If I were doing the same thing in a while loop, if I didnt put the three parts together at the beginning (when I first specified the loop) Im likely to forget something (probably the current = current->next) and Ill get an infinite loop the first time I run it. It ultimately costs me about 90 seconds of work, but it could have been avoided.

How were you planning on going about implementing your new looping style? Or is this for a new or variant language for which you are writing a compiler/interpreter?
 
I did intend to respond to your original post - I just couldnt think of anything that made a great deal of sense.

I think Ive just got so into the habit of the C Style for loop construct that I dont really think about it any more. More often than not Im tending to write loops over collections or similar anyway so I use the foreach construct more than a straight forward loop...

The one thing Im not sure of though is something you raised yourself - the fact that the parameters to the loop appear in a different order depending on which ones are present. However I cant see any other way of approaching the problem that wouldnt potentially be worse.
 
I actually like that for loop, mskeel. Ive used an equivalent while loop a few times, but never thought to use a for loop. It demonstrates that the for loop can be very flexible. What it really shows, though, is that the C-style for loop is more of an organization tool than anything. It is just a "while" loop that groups together statements related to control flow. (Either way, a DotNet purist would scoff at us for not using the ForEach loop.)

To answer your question, and this may ring a bell, In my spare time Ive been tinkering with a project that is now over a year old (and its still hardly functional). The original idea was simply a Windows command prompt replacement with (mostly) C-style syntax and functions as the equivalent of batch files. Someone (PD, I believe) kindly pointed out to me, early on in the project, that Microsoft was releasing a new command prompt packed with all of the nifty features I was planning to include in my own (except the syntax), hence my project has become a strictly academic endevour which has been moved to the back burner. And then I keep introducing features and concepts into the program, which only helps to slow the development process.

PD, to be frank, the order of the loop parameters doesnt bother me. What bothers me is that it may bother others. It seems quite intuitive to me, really. You specify only the information you need to, and in the most common sense order. But I know that not everyone thinks like me. In fact, I would venture to say that most people dont think like me. So I figured getting some extra input would make sense.
 
Back
Top