Using For Loop
Part of the Fundamentals section of Coddy's C# journey — lesson 64 of 69.
Iteration means going through elements one by one in a sequence. With arrays, we can access each element systematically using different methods.
The most common way to iterate through an array is using a for loop:
string[] fruits = {"apple", "banana", "orange"};
for (int i = 0; i < fruits.Length; i++) {
Console.WriteLine(fruits[i]);
}Output:
apple
banana
orangeChallenge
EasyCreate a program that starts with an array of words, and prints a new array containing only the words longer than 5 characters
Reminder: to print an array use the
Join()function:Console.WriteLine(string.Join(", ", arr));
Cheat sheet
To iterate through an array, use a for loop:
string[] fruits = {"apple", "banana", "orange"};
for (int i = 0; i < fruits.Length; i++) {
Console.WriteLine(fruits[i]);
}To print an array, use string.Join():
Console.WriteLine(string.Join(", ", arr));Try it yourself
using System;
public class Program {
public static void Main(string[] args) {
string text = Console.ReadLine();
string[] arr = text.Split(',');
// Write your code below
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3