Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Loops are similar to if statements, except they repeat execution if the condition is still true.
- There are 3 kinds of loops built into Java
- - while loop
- - for loop
- - enhanced for loop
- You can abruptly stop a loop by using break.
- for(...) {
- // execute some code
- if(...) {
- break; // exits loop - works with all loops
- }
- }
- // execution continues from here after breaking
- Or force the loop to skip to the next iteration by using continue.
- while(...) {
- // execute some code
- if(...) {
- continue; // restsart the loop, but at the next iteration
- }
- // code here isn't executed this iteration if continue is executed
- }
- You can also label loops to specify what loop to perform the break or continue on
- first:
- while(...) {
- second:
- for(...) {
- if(...) {
- continue first;
- } else if(...) {
- break second;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement