Difference between Array and Loop.
-
Hi, I want to know what is difference between Array and Loop.Is any one Knows,please tell me because i am little bit confuse??? Thanks... In Advance...
-
Hi, I want to know what is difference between Array and Loop.Is any one Knows,please tell me because i am little bit confuse??? Thanks... In Advance...
Arrays are used to store data. A single array can contain multiple items for for example numbers. Loop then again is used for example to iterate through an array (or anything "countable"). An example :
int[] someArray = new int[5]; // Array definition
for (int counter = 0; counter < 5; counter++) { // Loop
someArray[counter] = counter;
}Have a look at Arrays (C# Programming Guide)[^] and for[^] or while[^]
-
Hi, I want to know what is difference between Array and Loop.Is any one Knows,please tell me because i am little bit confuse??? Thanks... In Advance...
Two totally different things, but often used together. An array could be the floors in a building, while a loop could be like an elevator that you can use to visit those floors.
-
Two totally different things, but often used together. An array could be the floors in a building, while a loop could be like an elevator that you can use to visit those floors.
Nice example:thumbsup:
-
Hi, I want to know what is difference between Array and Loop.Is any one Knows,please tell me because i am little bit confuse??? Thanks... In Advance...
Hi, they are two completely different things: Array: An array is a set of homogeneous variables (I.E. they share the same type). it is declared in this way:
YourType[] yourArray = new YourType[noitems]
where
noitems
is the number of variables in the array. To access a single variable you need to use an index. This index starts from 0, not from 1, so the array indicies will go from 0 tonoitems-1
!!YourType yourArrayVariable = yourArray[yourIndex];
Please keep in mind that if
yourIndex
points to a variable which does not exists in the array, a IndexOutOfRangeException will be thrown. Arrays can also have multiple dimensions:YourType[,] yourTable = new YourType[6,2];
it is a declaration of a table with 6 rows and 2 columns. You can imagine a single-dimension array as a single row, a two-dimensional array as a table and a three dimensional array as a cube. There are also jagged arrays. While if you decalre your multi-dimensional array just like the code above you'll get a rectuagular table, with jagged arrays (or array of arrays), you can define your personal dimension for each element.
YourType[][] jaggedArray = new YourType[3][];
jaggedArray[0] = new YourType[3]; //<----- jaggedArray[0] is a single-dimensional array of type YourType
jaggedArray[0][2] //<----- This is a single variable of type YourTypeLoops are a completely different thing.
A loop enables you to do something repeatedly while a condition is true.While loops will do the instructions inside their body while a condition is true. The condition is evaluated before the loop starts and, if it is false, the loop does not start.
while (condition){
//do something if condition is true
}There is another version of while which is the do...while loop. The only difference is that the condition is evaluated after the loop is executed. I.E. it is evaluated at the end, so the loop is executed at least once before evaluating the condition.
do{
//do something
}while(condition);There is the for loop which is controlled by a variable which is usually an integer or, generally, a number:
for (declaration;condition;instruction){
//do something
}for(int i = 0;i<5;i++){
}The for loop has 3 different parts: the declarati