Learn Java: Loops

Nice work! Let’s iterate over what you’ve just learned about loops:

  • while loops: These are useful to repeat a code block an unknown number of times until some condition is met. For example:
int wishes = 0; while (wishes < 3) { // code that will run wishes++; }
  • for loops: These are ideal for when you are incrementing or decrementing with a counter variable. For example:
for (int i = 0; i < 5; i++) { // code that will run }
  • For-each loops: These make it simple to do something with each item in a list. For example:
for (String inventoryItem : inventoryItems) { // do something with each inventoryItem }


Feel free to play around with loops, arrays, and ArrayLists in the code editor.

Folder Icon

Take this course for free

Already have an account?