subject: Is Java Getting You Into Loops? [print this page] If you are new to the world of J2EE Application Development, then the concept of loops can be quite tricky for you. There are several developers who struggle everyday to understand loops right. Java supports several different kinds of loops and unwinding all of them is a requisite for a good Java developer. This post discusses the concept of loops in detail and introduces you to all major loops used in Java programming.
There are three major concepts that form the building blocks of Java programming. These are iteration, sequence and selection. The concept of sequence is comprehended well by majority of developers. When statements come one after another, it is known as a sequence. For instance, the following statements form a sequence:
System.out.println ("hey");
System.out.println ("goodbye");
Understanding the concept of selection is also fairly easy. All you need is an if/switch statement and you can exercise full control on your code. If you have been into development for quite some time, you must be well aware about the importance of if statements. All major development ventures are practically impossible without the use of if statements at the right instances.
Iteration is the final essential concept of J2EE application development, which will also be the centre of focus of this post. The basic concept of iteration deals with repetition of a sequence of statements in a loop, until a said condition is met. Once the condition is met, iteration terminates and the loop is closed.
There are several different types of loops brought into use in Java development. Some of the major and most commonly used loops are listed below:
While Loops: While loop is used for simply testing a condition that has been listed in the loop. If the condition is not true, the iteration will not continue any further. Heres an example: