|
Arrays and Loops
Arrays
An array is a simple data stucture with the following features:
- Able to store a finite number of data values of the same type.
- Storage is divided into positions.
- Each position can store one value.
- Each position is labeled with an integer number, starting with 0.
- The stored values can be referenced by their position number.
The following is a list of commonly used terms related to the an array:
- Length: The maximum number of positions available in an array.
- Element: Each data value stored in an array.
- Index: The position number of each element.
There are several syntaxes involved to define an array, create an array, assign
an array, and represent an array elelement:
array_declaration_statement:
type[] array_name;
array_creation_expression:
new type[length]
array_assignment_statement:
array_name = new type[length]
array_element expression:
array_name[index]
The array declaration statement defines the name of the array, and the type of
the data value the array can store. Once an array name is defined, it is ready
to be assigned with an array of the same type.
The array creation expression creates an array of the specified type and length.
The array assignement statement assigns an array to an array name of the same type.
The array element expression represents an array element, which can be used in the
same way as a variable of array's type.
The following program is a good illustration of the different array syntaxes:
class Arrays {
static void Main() {
int[] primes;
primes = new int[10];
primes[0] = 2;
primes[2] = 5;
System.Console.WriteLine("The 3rd prime number is: {0}",
primes[2]);
double[] temporatures = new double[3];
temporatures[0] = 70.0;
temporatures[1] = 75.0;
temporatures[2] = 73.5;
System.Console.WriteLine("Forecast for tomorrow's high"
+ " temporature: {0}", temporatures[2]);
}
}
Output:
The 3rd prime number is: 5
Forecast for tomorrow's high temporature: 73.5
Please note that:
- The first element has an index of 0, not 1.
- You don't have to assign values to all array elements. primes[1] has no value
in the above program.
- You will get an outside-the-bounds error, if you try to use primes[10], because
you are trying to access the 11th position of the array, which doesn't exist.
- We are allowed to join the assignment with the declaration statement, like
"double[] temporatures = new dobule[3];".
"for" Loops
A "for" loop is a "for" statement, by which a group of embeded statements will repeatedly
executed like a loop until the controlling condition reaches false. Here is the syntax of
a "for" statement:
for statement:
for (initiation_statement; logical_expression; iteration_statement) {
embeded_statements
)
When a "for" statement is encountered in an execution flow, it will follow the algorithm
bellow:
- Step 1: Executed the initiation statement.
- Step 2: Evaluate the logical expression.
- Step 3: If the resulting value of the logical expression is true, run all the
embeded statements; otherwise, end this "for" statement.
- Step 4: After executing the embeded statements, if they are executed as part of
Step 3, executed the iteration statement, and loop back to Step 2.
The following program uses a "for" loop to calculate the sum of integers from 1 to 10:
class ForLoops {
static void Main() {
long total;
total = 0;
int i;
for (i=1; i<=10; i=i+1) {
total = total + i;
}
System.Console.WriteLine("Sum of 1, 2, ..., 10: {0}", total);
}
}
Output:
Sum of 1, 2, ..., 10: 55
"while" Loops
A "for" loop is a "while" statement, which is identical to the "for" statement without
the initiation_statement and the iteration_statement. Here is the syntax of
a "while" statement:
while statement:
while (logical_expression) {
embeded_statements
)
When a "for" statement is encountered in an execution flow, it will follow the algorithm
bellow:
- Step 1: Evaluate the logical expression.
- Step 2: If the resulting value of the logical expression is true, run all the
embeded statements; otherwise, end this "while" statement.
- Step 3: After executing the embeded statements, if they are executed as part of
Step 2, loop back to Step 1.
Comparing the "while" statement with the "for" statement:
- The "while" statement is simpler. But the execution of the embeded statements
should affect the result of the logical expression. Otherwise, the logical
expression could be always resulted in true, and the loop becomes an endless
loop.
- The "for" statement offers a structure matches nicely with the array stucture,
where the array index can initialized, checked against the length of the array,
then increamented by 1 at the end of each interation.
|