Just hoping to get some input for a DotNet project I'm 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:
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:
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 VB's for loops).
If it makes any difference, this simplified loop would be used primarily in scripting rather than compiled code. Any thoughts are welcome.
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 aren't 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 I'm at " + i.ToString());
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 VB's for loops).
If it makes any difference, this simplified loop would be used primarily in scripting rather than compiled code. Any thoughts are welcome.