For in Minitab
The for
loop is Java’s primary looping construct. Here are some basic types of for
loops.
When you run this program, you’ll see:
In Java, we use while
loops for the most basic type of loop with a single condition. The classic for
loop syntax is similar to many other languages. Java doesn’t have a built-in range
function like some other languages, so we use traditional for
loops to achieve similar functionality.
The infinite loop in Java is typically written as while (true)
, and you can use break
to exit the loop or return
to exit the method.
The continue
statement works the same way in Java as in many other languages, allowing you to skip to the next iteration of the loop.
We’ll see other forms of iteration later when we look at enhanced for
loops (for-each loops), which are useful for iterating over collections and arrays.