C-SHARP - 3.11 Loop (demo)
Time to write our first loop in C#. There are three loop constructs available — while, for and foreach — that broadly do the same thing in different ways. We start with while and cover for and foreach in a separate video. The goal: create a string array of first names and print every name using a while loop.
First, declare the array: string[] tab_prenom = new string[] { "Jibril", "Jason", "Thibault", "Eastman" };. Then a counter variable that doubles as the array index: int compteur = 0;. Developers traditionally name it i, but compteur makes the role obvious. The while condition is "while the counter is below the array length": while (compteur < tab_prenom.Length). Notice the .Length property — without it you would be comparing a number to an array, which is invalid; Length returns the number of items so we end up comparing two numbers.
Inside the loop body
- Print the current name:
Console.WriteLine(tab_prenom[compteur]);— the[compteur]index changes at every pass, so each iteration prints a different element. - Increment the counter:
compteur++;— without this, the loop runs forever. - Result: the four names print in order until
compteurreaches 4 and the condition becomes false.
The classic mistake is forgetting the increment. With compteur stuck at 0, the condition 0 < 4 stays true forever, the loop runs the same instruction infinitely and the program hangs — that is an infinite loop. Always make sure the loop body changes the variable used in the condition. With this pattern under your belt you can already iterate over arrays, lists or any sequence. In the next video we cover for and foreach, which streamline the same logic with less boilerplate.
Summary
This lesson demonstrates how to implement a while loop in C# to iterate through an array of names. The tutorial covers creating an array, initializing a counter variable, writing the while loop condition to check the array length, and using the counter as an index to access and display each array element. A critical warning is given about the importance of incrementing the counter inside the loop to avoid infinite loops.
Key points
- Create arrays using the syntax: type[] arrayName = new type[] { values }
- Use a counter variable initialized to 0 to track iterations and serve as an array index
- Write while loop conditions comparing the counter to array.Length to process each element
- Access array elements using the counter variable as the index: array[counter]
- Always increment the counter inside the loop to prevent infinite loops
- The while loop is one of three main loop types in C# alongside for and foreach loops
FAQ
Why do we use array.Length in the while loop condition?
array.Length returns the number of elements in the array, allowing us to compare it with the counter. This ensures the loop iterates exactly once for each array element without exceeding the array bounds.
What happens if we forget to increment the counter inside the while loop?
This creates an infinite loop. Since the counter never increases, the condition remains true forever, and the loop executes its instructions indefinitely, freezing the program.
How does the counter variable help access each array element?
The counter acts as an index in the array brackets. As it increments from 0 to array.Length-1, each iteration accesses a different array element (array[0], array[1], etc.) allowing you to display or process each value sequentially.