C# Studies
Loop - FOR
Base structure
- It has 3 parts:
- Counter
- Condition
- Increment
Let’s look at the following example:
for (int i = 0 ; i <= 5 ; i++)int i = 0is the counter, starting at zeroi <= 5is the condition, which runs until the value reaches 5i++is the increment, where the value of i goes up one by one
Other examples:
for (var i = 0 ; i <= 5 ; i++) // Counts from 0 to 5
for (var i = 0 ; i < 5 ; i++) // Counts from 0 to 4
for (var i = 5 ; i >= 0 ; i--) // Counts from 5 down to 0Now the complete example of a For that prints the value of i
for (int i = 0 ; i <= 5 ; i++){ Console.WriteLine(i); // Every time it gets here it prints i}
// OUTPUT:// 0// 1// 2// 3// 4// 5For each
With arrays we can use a different kind of for called foreach, which lets us iterate over the values stored in an array
Let’s look at the following array:
int[] numbers = { 10, 20, 30, 40 };We want to work with the values inside this array. Instead of choosing a position, we can iterate over all of its values
int[] numbers = { 10, 20, 30, 40 };
foreach (int num in numbers){ Console.WriteLine($"Number: {num}"); // prints every value}