Thursday, February 24, 2011

Arrays And Loops

Arrays are variables that can hold a list of data, instead of one. After all it would be annoying to create 20 variables to hold a list of numbers.

First create a new project called "ArraysAndLoops".

Start by typing:
int[] listOneOfNumbers = new int[3] {25,31,43};
int[] listTwoOfNumbers = new int[3];
listOneOfNumbers[0] = 25;
listOneOfNumbers[1] = 31;
listOneOfNumbers[2] = 43;
You know that every variable needs a data type. Every array also needs a data type and in this case it will be of the basic data type int. The rule in C# is to put the "[]" signs after the data type to make it an array. The "new int" is to create the array with the basic data type and give it a spot in the memory. "[3]" shows that your array will hold 3 elements of data. Try to assign data to a non-existent fourth element and your application will crash. There are two ways to assign data to each element and both are correct, so just pick one. Arrays begin with element #0 and not with element #1.

"int[] listOneOfNumbers = new int[3] {25,31,43};" will assign data to each element and can only be used when creating an array. The following numbers will be assigned to each a specific element based on the order of the numbers in the array: 25, 31 and 43. The number 25 will be assigned to element #0, 31 to element #1 and 43 to element #2.

int[] listTwoOfNumbers = new int[3];
listOneOfNumbers[0] = 25;
listOneOfNumbers[1] = 31;
listOneOfNumbers[2] = 43;
Above code will first create an array with only 0s as data in its elements. Then you assign data to each element separately. You start with element #0 and end with element #2. In total there are 3 elements to assign data to, just like you typed when you created your array.

You want to read all the elements in the array and want to show them on your screen. This is done by using a loop that goes through every element in the array. Add the following code below "listOneOfNumbers[2] = 43;":
for (int i = 0; i < listOneOfNumbers.Length; i++)
Console.Write(listOneOfNumbers[i] + "\n");

A for loop consists out of 3 parts. First you need to initialize a variable. This variable is used to check how many times your code in the loop has been repeated.

The second part is the condition. Your application checks the condition if it still applies every time it loops from the beginning. In the code you just added the condition is "i < 3" (Your array has 3 elements), so your application loops as long as the variable "i" is smaller than 3. This means that the loop will go through the numbers 0, 1 and 2. The third and last part is the step. This part increments your variable "i" every time it loops or else the condition would be useless, because the variable "i" would stay on 0.

The first time your loop its variable "i" will contain the number 0. This is perfect, because you need to read element #0 in your loop. After reading the element and getting the data your application will write the number 25 on your screen. The loop is now done and will increment the variable "i" in the step part of the loop. After that it will check if the condition is still true. The variable "i" now holds the number 1. The loop will continue, because 1 is smaller than 3.

Now your loop's variable "i" holds the number 1 and it will execute the code in the loop again. It will now read element #1 and write the number 31 on your screen. It will increment the variable "i" again and will now check if 2 is smaller than 3. It is, so it will loop again.

Variable "i" now holds the number 2, so it will read element #2 and write the number 43 on your screen. After incrementing the variable "i" again it will check the condition. 3 is not smaller than 3, so the loop stops. This means that "Console.Read();" will be executed and this is where your application ends.

Run your application by pressing F5 on your keyboard and you should see the numbers 25, 31 and 43 on your screen.

Source Code ArraysAndLoops Project

>> Switch Statements

No comments:

Post a Comment