Java is a sequential structure program that can only be executed once. If you want to perform the same operation multiple times, you need to use a loop structure.
There are three main loop structures in Java:
while loop
do...while loop
for loop
Introducing an enhancement mainly for arrays in java5 type for loop.
1. while loop
while is the most basic loop, its structure is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
2. do...while loop
For the while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to execute it at least once even if the conditions are not met.
do…while loop is the same as while loop. The difference is that
do…while loop will be executed at least once.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
3. for loop
Although all loop structures can be expressed by while or do...while, java provides another statement (for loop), Made some loop structures simpler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
4. java enhanced for loop
java5 introduces an enhanced rot loop mainly used for arrays.
java enhanced for loop syntax format is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
The above is the detailed content of How to implement loop in java. For more information, please follow other related articles on the PHP Chinese website!