Showing posts with label c# loops. Show all posts
Showing posts with label c# loops. Show all posts

Tuesday, September 25, 2012

Beginner in C#: using fixed one dimensional Arrays

What is an Array?

An array is simply a place to store an amount of items, that can be used later easily.

Defining a fixed Array

First, you must specify the type of items to store in your array, lets say for example you want to store a list of integers. Then, the number of items (integers) you want to store.
The following code snippet creates an array of 4 integers.
int[] myFirstIntArray = new int[4];

Storing items in your array

In C#, an array index starts at 0, and ends at the array length minus one.
To store data in a predefined array, the basic way, just set the value of each item in your array, using the equal sign!
Let's consider our previous example. we want to store in it the following information:
  1. my age.
  2. my birth-year.
  3. my birth-month.
  4. my birth-day.
The code below will do the task.

myFirstIntArray[0] = 19 ; //19 years old
myFirstIntArray[1] = 1993 ;
myFirstIntArray[2] = 7 ; //July
myFirstIntArray[3] = 21 ;

Of course, you may need to store a big amount of items and you're not gonna put them all one by one, so in this case we usually use the Loops.

A good example: Let's say we want to get the list of the first 100 prime numbers. Here we are going to use a do-While loop.

            int[] primeNbrs = new int[100];
            int number = 2;
            int index=0;
            do
            {
                int i = 2;
                while ((i < number) && (number % i != 0)) i++;
                if (i == number)
                {
                    primeNbrs[index] = number;
                    index++;
                }
                number++;
            } while (index < 100);
If you have any questions, COMMENT!

Wednesday, September 19, 2012

Beginner in C#: Understanding loops


What is a loop in programming?


A loop is as statement that helps you to execute a piece of code repeatedly, a known number of times or until a certain condition become true.
For example, if I said I'm going to walk exactly 500 steps, I'm making a loop about one step repeated 500 times. But if I said I'm going to walk until my dad calls me, here I don't know when exactly my dad will calls me, so this is a loop depending on a particular condition: "my dad is calling me", when it becomes true I will stop walking.

Loops in C#


The 3 most frequently used loops in C# are the following:

the "for" loop

The for loop is usually used to repeat a code N times, N could be a variable or a fixed integer (int).
here's how you write it:

int i; // Declaring an integer with the name 'i'
for(i=0;i<N;i++)
{
// YOUR CODE HERE
}

the "while" loop

The while loop is used to repeat an action until the condition (of type bool) becomes true. 
usage:
while(!condition)
{
// YOUR CODE HERE
}

the "do-while" loop

The do-while loop is just like the while, the only difference is that by its usage we ensure that your CODE is executed at least once.
usage:
do
{
// CODE
}while(!condition);
Its equivalent to:
// CODE
while(!condition)
{
// CODE
}