I have this loop:
for (int i = 1; i < 10; i++)
But instead I would like to have i for just numbers 1,2,4,5 and 7 and I will hardcode this.
Is there a way I can do this with something like an array?
A for loop doesn't increment anything. Your code used in the for statement does. It's entirely up to you how/if/where/when you want to modify i or any other variable for that matter.
No, the for loop will perform an exit condition in order to break the loop.
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 })
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With