Loops in Java
- for loop.
- Enhanced for loop.
- while loop.
- do-while loop.
Vishalini.R Answered question September 7, 2023
Java has three types of loops i.e. the for loop, the while loop, and the do-while loop. for and while loops are entry-controlled loops whereas do-while loop is an exit-controlled loop.
Sandhya Answered question September 7, 2023
for Loop:
- The
for
loop is used when you know the number of iterations in advance. - It consists of an initialization statement, a condition for termination, and an update statement, all separated by semicolons.
while Loop:
-
- The
while
loop repeatedly executes a block of code as long as a given condition is true. - It checks the condition before each iteration.
- The
do-while Loop:
-
-
- The
do-while
loop is similar to thewhile
loop, but it checks the condition after executing the block of code. This guarantees that the code block will be executed at least once.
- The
-
enhanced for Loop (for-each Loop):
-
-
-
- The enhanced
for
loop is used to iterate over elements in an array or other iterable collections. - It simplifies the syntax for iterating over elements without needing explicit indexing.
- The enhanced
-
-
Shathana. S.R. Answered question September 7, 2023