Section 8.5. Concepts Summary


[Page 284 (continued)]

8.5. Concepts Summary

In this chapter we worked with one-dimensional arrays, while loops, for loops, and conditionals.

8.5.1. Arrays

Arrays are used to store many pieces of data of the same type. They allow you to quickly access a particular item in the array using an index. If you couldn't use an array, you would have to create a separate variable name for each piece of data.


[Page 285]

To declare a variable that refers to an array use the type followed by open '[' and close ']' square brackets and then the variable name.

SoundSample[] sampleArray;


This declares an array of SoundSample objects. The value stored at each position in the array is a reference to a SoundSample object.

Arrays are objects and you can find out how large an array is using:

arrayReference.length


Notice that this isn't a method call but instead it accesses a public field.

You can get an element of the array using arrayReference[index]. Where the index values can range from 0 to arrayReference.length-1.

8.5.2. Loops

Loops are used to execute a block of statements while a boolean expression is true. Most loops have variables that change during the loop which eventually cause the boolean expression to be false and the loop to stop. Loops that never stop are called infinite loops.

We used three types of loops in this chapter: for-each, while, and for. The while loop is usually used when you don't know how many times a loop needs to execute and the for and for-each loops are usually used when you do know how many times the loop will execute.

The for-each loop was introduced in Java 1.5 and loops through all the elements of an array one at a time.

SoundSample[] sampleArray = this.getSamples(); int value = 0; // loop through all the samples in the array for (SoundSample sample : sampleArray) {   value = sample.getValue();   // do something to the value   sample.setValue(value); }


This declares a variable sample that is of the type SoundSample and each time through the loop the sample variable will refer to a different element of the array until all the elements have been processed.

The while loop has the keyword while followed by a boolean expression and then a block of statements between an open and close curly brace. If the boolean expression is true, the body of the loop will be executed. If the boolean expression is false, execution will continue after the body of the loop (after the close curly brace). If you just want to execute one statement in the body of the loop, then you don't need the open and close curly braces, but you should indent the statement.


[Page 286]

while (boolean expression) {    statement1;    statement2;    ... }


If you use a while loop to execute a block of statements a set number of times, you will need to declare a variable before the while and that variable will need to be changed in the body of the loop. You may also need to declare other variables that you use in the loop before the while. Don't declare variables inside the loop because you will use more memory that way.

SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; int index = 0; int value = 0; // loop through all the samples in the array while (index < sampleArray.length) {   sample = sampleArray[index];   value = sample.getValue();   // do something to the value   sample.setValue(value);   index++; }


The for loop does the same thing as a while loop, but it lets you declare the variables that you need for the loop, specify the boolean expression to test, and specify how to change the loop variables all in one place. This means you are less likely to forget to do each of these things.

SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; int value = 0; // loop through all the samples in the array for (int index = 0; index < sampleArray.length; index++) {   sample = sampleArray[index];   value = sample.getValue();   // do something to the value   sample.setValue(value); }


8.5.3. Conditional Execution

To conditionally execute one statement use the if keyword followed by a boolean expression inside of an open and close parenthesis. Put the statement that you only want executed if the boolean expression is true on a new line and indent it. If the boolean expression is false, execution will continue with the next statement.


[Page 287]

if (boolean expression)    // statement to execute if the boolean expression is true    statement // next statement statement


To conditionally execute a block of statements, use the if keyword followed by a boolean expression inside of an open and close parenthesis. Put the statements to be executed when the boolean expression is true inside of an open and close curly brace. Indent the statements to make it easier to visually see that these statements will only be executed if the boolean expression is true. If the boolean expression is false, execution will continue with the statement following the close curly brace.

if (boolean expression) {    statements }


If you want to execute one block of statements if the boolean expression is true, and another if it is false, use the else keyword as well. Put the statements that you want to execute when the boolean expression is true inside of an open and close curly brace after the if(booleanExpression). Next, add the keyword else and put the statements that you want executed when the boolean expression is false inside of an open and close curly brace.

if (boolean expression) {  statements } else {  statements }




Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net