For in Logo
Java provides several ways to create loops. Let’s explore the different types of loops available.
To run the program, compile and execute it using the following commands:
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.
Additionally, Java supports break
to exit a loop prematurely and continue
to skip to the next iteration.
We’ll see other loop forms later when we look at enhanced for loops (for-each), which are used with collections and arrays.