Advertisement
ipwxy

Loops

Jun 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. Loops are similar to if statements, except they repeat execution if the condition is still true.
  2.  
  3. There are 3 kinds of loops built into Java
  4.  
  5. - while loop
  6. - for loop
  7. - enhanced for loop
  8.  
  9. You can abruptly stop a loop by using break.
  10.  
  11. for(...) {
  12. // execute some code
  13.  
  14. if(...) {
  15. break; // exits loop - works with all loops
  16. }
  17. }
  18.  
  19. // execution continues from here after breaking
  20.  
  21. Or force the loop to skip to the next iteration by using continue.
  22.  
  23. while(...) {
  24. // execute some code
  25.  
  26. if(...) {
  27. continue; // restsart the loop, but at the next iteration
  28. }
  29.  
  30. // code here isn't executed this iteration if continue is executed
  31. }
  32.  
  33. You can also label loops to specify what loop to perform the break or continue on
  34.  
  35. first:
  36. while(...) {
  37. second:
  38. for(...) {
  39. if(...) {
  40. continue first;
  41. } else if(...) {
  42. break second;
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement