For in Mercury
Java provides several types of loop constructs. Here are some basic types of loops.
To run the program, compile it and then use java
:
Java provides several loop constructs:
while
loop: Executes a block of code as long as a condition is true.for
loop: A compact way to iterate over a range of values.do-while
loop: Similar to while
, but guarantees at least one execution of the loop body.- Enhanced
for
loop (for-each): Used to iterate over arrays or collections.
We’ll see some other loop forms later when we look at iterating over collections, working with streams, and other data structures.