New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.
|
Get the latest Java books |
|
h t t p : / /w w w . j a v a c o f f e e b r e a k . c
o m /
|
Menu Articles Using Java Applets Looking for Java resources? Check out the Java Coffee Break directory! |
Is Java driving you loopy?
There are three basic building blocks of programming :-
Most programmers will be familiar with sequence. A sequence of programming statements are executed one after the other. For example, the following two lines can be executed sequentially System.out.println ("hello"); System.out.println ("goodbye"); Selection is also fairly simple. By using The final concept, iteration, is the focus of this article. This one is a little more tricky, as there are quite a few different times of iteration statements in Java. The basic concept behind iteration, is that a sequence of statements is repeated until a certain condition is met. When this condition is met, the iteration terminates, and the loop is over. 'for' loopsThe simplest type of loop uses the // for loop, from 1 to 5 for (int i = 1; i <= 5; i++) { System.out.println ("count : " + i); } There are several components to a A check to see if the condition equates to It is also worth noting that each of the components of a for loop is entirely optional. For example, our loop could be rewritten as the following: - // This may be more useful, as the variable // i is created outside the scope of the // loop. int i = 1; // for loop, from i to 5 for (; i <= 5; i++) { System.out.println ("count : " + i); } 'while' loopsA while loop differs from the for loop, in that it only contains a condition, and that the condition is tested at the beginning of the loop. This means that if the condition evaluates to false, it will not be executed at all. We call this a pre-tested loop, because the test is made before executing the sequence of statements contained within. int i = 1; while (i <= 5) { System.out.println ("count : " + i); i++; } 'do...while' loopsA variation on the while loop is the post-tested version. Two keywords are used for this loop, the do and the while keywords. int i = 1; do { System.out.println ("count : " + i); i++; } while (i <= 5) There is no difference between a while, and a do...while loop other than pre-testing and post-testing. Terminating loops abruptlySometimes it is necessary to stop a loop before its
terminating condition is reached. For example, a loop that searched
through an array for a particular entry should not continue once that
entry is found - to do so would only waste CPU time and make for a
slower program. Other times, you may want to skip ahead to the next
iteration of the loop, rather than performing additional processing.
Java provides two keywords for this purpose : breakThe // Check to see if "yes" string is stored boolean foundYes = false; for (int i = 0; i < array.length; i++) { if (array[i].equals("yes")) { foundYes = true; break; } } continueUnlike the for (int i = 0; i < array.length; i++) { if (array[i] == null) // skip this one, goto next loop continue; else { // do something with array[i] ....... } } While this example was unnecessary, since we had an if
statement to separate program flow, there are some complex algorithms
where SummaryLoop don't need to be difficult, providing you keep in minds these simple concepts: -
|
||||
|