Is there a way to code a for loop so that it doesn't increment through a sequence?
You could use an array to give the numbers you want like this
int[] loop = new int[] {1,2,4,5,7};
foreach(int i in loop)
Console.WriteLine(i);
Or do it inline which is not as clean when the list of values grows in my opinion
foreach(int i in new int[] {1,2,4,5,7})
Console.WriteLine(i);
foreach (int i in new[] { 1, 2, 4, 5, 7 })
{
}
Basically the answers here are correct, just because you asked explicitly for a for
instead of a foreach
loop:
int[] loop = new int[] { 1, 2, 4, 5, 7 };
for (int i = 0; i< loop.Length; i++)
{
Console.WriteLine(loop[i]);
}
https://dotnetfiddle.net/c5yjPe