Arrays and Loops

In this article, we will cover the basics of Arrays and Loops. By the end of this article, you should have a clear idea of what arrays are and when to use them as well as the C# syntax and operations. You will also understand how to use loops instructions to iterate through all types of collections.

Arrays data structure

An array is a data structure consisting of a collection of items of the same type. It is useful to store items that have meaning together. For example, if we have an inventory of cars in a dealership system we could store all the cars in an array and then operate with them to display on a webpage or calculate the amount of cash the company has invested in its inventory.

Independent variables for quantity of apples, grapes and oranges, vs an array of fruits all together.

Above image is a nice example of how you can manage an inventory of fruits by having a lot of independent variables or instead use an array and put all the info together. You would have to know that the first position of the array corresponds to apples, second to oranges and last to grapes of course. We will get into that later, for now lets work with simpler arrays that contains only numbers or strings. Lets take a look at this example in C# code:

Some important characteristics about arrays:

  • They are 0 based index. The first item is at position 0.
  • They are static structures, you need to know how many items (maximum) your array will contain when creating it.
  • In typed languages like C#, the items should all be of the same type.
  • It is a chunk of memory that contains all the items together, super fast for accessing items (direct access) through the index. We will later get to other structures that do not have this property.

Loops, iterate through everything!

Loops, or cycles, are instructions that allow us to repeat an action a lot of times without having to write the same code over and over. A common use case of loops is to iterate through collections.

For example, let’s imagine we are coding some functions of a calculator on our console app. And we want to allow the user to enter 5 numbers and then add all of them and show the result. We would have to define 5 variables and ask for input one by one, that would be repeating our code 5 times. Or we could have an array of inputs and a for loop that runs 5 times and its action is to ask for input and store it in our array. The code for this example is the following:

Understanding the for instruction

The for instruction uses the reserved word “for” and then between () three sentences. The first one is only executed once, at the beginning of the cycle, and is usually utilized for declaring and initializing the variable used for indexing in our loop. The second sentence is executed once every iteration and its purpose is to check when to stop the cycle, it has to return true or false. When the condition is false the loop finishes. And the last sentence is executed once for every iteration and is usually used for updating our index.

In the code above we can see how, using variables, we have to declare each one of them and most important we have to repeat the Console.WriteLine sentence 5 times. Instead, using the for loop we just write it once. It might not seem like a lot in this simple example but imagine if instead of 5 numbers it was 100 or 1000. Not only that, what if instead of only having to ask for a number (only one line of code) we have to go to a database a bring back info and process it. That would be a couple of lines of code and if we don´t use a loop we have to repeat that for each item and the code gets messy and long. And if we ever have to modify it we will have to do it for each item. That is a very bad practice. In development, we try to always write a piece of code once, never repeat yourself.

As an extra exercise think how could you re write the code with the loop implementation to avoid using the array. If we don´t have to store the inputs the user entered but only calculate the sum of the five of them we could have some sort of accumulator and display the result after the loop. Try to write the code to do that!

Something very important to note is that “for loops” are meant to be used when we know how many times we want to repeat the action. In the example, it was five and that was defined in the cut condition (second sentence). What happens when we don´t know how many times we want to iterate? Well for that we would use a while loop.

The While Loop

A while loop is a cycle that repeats while the condition is true. It is particularly useful when we have an external input to stop the cycle. Let’s look at an example:

In the example above we ask the user for numbers until he enters a 0, and then we stop adding them together and output the result. An interesting point to note here is that the condition on the while could be simplified because bools variables already have a value of true or false. I added the == true to make it more clear, but you could remove it and it would work the same way, try it out!


By now you should have a decent idea of how to create arrays, add values to them and work with for and while loops. I will leave you with some exercises to practice what you have just learned in the next fiddle.

That will be all for this article. In the next one we will start working with dynamic collections, we will cover Lists.

Click on the image above so you don’t miss out on future content.