Article

Is Java Getting You Into Loops?

Topic: SoftwarePublished November 7, 2012

Reader stats

1,132 views

Article rating

No ratings yet

Reader rating appears publicly after enough eligible article ratings.

Rate this article

Sign in to rate this article.

Sign in to rate this article

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. Here's an example:

int i = 1;

while (i <= 5)
{

System.out.println ("count: " + i);

i++; }

For Loops: This is the simplest of all loops. It uses the 'for' statement to complete the iteration process using a series of sequences. By default, a 'for' loop includes a counter which has a defined starting point and a defined ending point. Here is an instance:

// for loop, from 1 to 5rnfor (int i = 1; i <= 5; i++)
{

System.out.println ("count: " + i); }

Do-While Loops: This is a version of the while loop itself. It includes the use of two keywords- do and while. For instance:

int i = 1;
dorn{

System.out.println ("count: " + i);

i++; }rnwhile (i <= 5)

Termination of Loops:

At times, during the programming you have to abruptly terminate a loop before the statement is fulfilled. For this purpose, Java uses two conditions: break and continue.

Break: This keyword can be used in case you want an early termination of the loop. Consider the following example for a better understanding:

// Check to see if "yes" string is storedrnboolean foundYes = false;

for (int i = 0; i < array.length; i++)
{

if (array[i].equals("yes"))

{

foundYes = true;

break;

} }

Continue: This keyword, unlike break, doesn't break the loop. Instead, it skips the loop and moves onto another iteration in the programming. Consider the following example:

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] ...

} }

The concept of loops is worth understanding if you want to make your J2EE application development worthwhile.

Article author

About the Author

Steve Graham is a Java expert who has industry expertsie in Java web development and J2EE application development. He is currently supervising Enterprise Java development projects as a senior developer at Xicom Technologies ltd, an offshore software development companmy.

Further reading

Further Reading

4 total

Article

Organizations are starting to scale their cloud native operations. And as they do, the inefficiency of managing dozens of isolated clusters has become an evident problem. As the clusters continue to sprawl, businesses must unite diverse workloads onto shared infrastructure. This is because companies need better resource utilization and centralized governance among other things. But it is imperative to remember that going from a single tenant to a multi-tenant environment need

March 12, 2026

Article

It has been for everyone to see the short product lifecycles and a pressing need for rapid technical scalability that have come to define the modern startup ecosystem. For early-stage companies, the challenge is no longer just conceptualizing a solution. But they must also carry it out with enough precision to withstand high market volatility and fierce competition. We know that internal teams concentrate on core business strategy and fundraising. That still leaves us with th

March 12, 2026

Article

In today’s regulated and data-driven environments, organizations are under constant pressure to ensure that temperature and environmental conditions remain within defined limits. Even small fluctuations can result in product loss, compliance violations, or operational downtime. As a result, many facilities are moving away from manual checks and standalone sensors and adopting comprehensive environmental monitoring solutions instead. An environmental monitor provides rea

March 5, 2026

Article

Organizations have come to rely heavily on large amounts of data in today's competitive markets. But to what end? For starters, to inform strategic decisions and power machine learning models. It goes without saying that the value of these digital assets is completely dependent on the accuracy of the underlying data. So, when data is fragmented or inconsistent across departments, you will obviously have inaccurate reporting and operational inefficiencies at your hands. This c

March 2, 2026