⚡0 XP🔥0

Loops - for, while, do-while and for-each

Lesson 5 of 15 ⏱ 6 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs Java

Repeat code with for, while, do-while and the enhanced for loop, use break, continue and labelled break, and solve classic number and pattern problems.

The for loop#

for (initialisation; condition; update) {
    // body
}
▶ Try it YourselfJava
Ctrl + Enter to run
Output
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
▶ Try it YourselfJava
Ctrl + Enter to run
Output
15! = 1307674368000
Sum of even numbers 1-100 = 2550
5 4 3 2 1 

The while loop#

Use while when the number of iterations depends on a condition, not a count:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
12321 is a palindrome
Money doubles in 10 years

The do-while loop#

The body runs first, then the condition is checked - perfect for menus and "ask until valid" input:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
120 is invalid, enter 0-100
-5 is invalid, enter 0-100
Accepted: 86

The enhanced for (for-each) loop#

To visit every element of an array or collection without an index:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
I am learning C
I am learning C++
I am learning Java
I am learning Python
I am learning SQL
Total = 316

break, continue and labelled break#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
1 3 5 7 9 11 
Found 42 at row 1, col 1
हिंदी में समझें

break loop से बाहर निकल जाता है, continue बाकी code छोड़कर अगले चक्कर (iteration) पर चला जाता है। Nested loops में बाहर वाले loop से निकलना हो तो उसे label दें (outer:) और break outer; लिखें।

Nested loops and patterns#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
*
**
***
****
*****
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

String.repeat(n) (Java 11+) saves an inner loop. In exams you may be asked to write the inner loops explicitly - both approaches give the same output.

Prime numbers - a classic#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
15 primes below 50

Summary#

  • for for counted loops, while for condition loops, do-while when the body must run once.
  • The enhanced for (for (T x : collection)) reads every element.
  • break exits, continue skips; labels let you break out of nested loops.
  • Digit tricks (% 10, / 10) and nested loops solve most classic problems.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Armstrong numbers

An Armstrong (narcissistic) 3-digit number equals the sum of the cubes of its digits (153 = 1³ + 5³ + 3³). Print all 3-digit Armstrong numbers on one line separated by spaces.

Expected output:
153 370 371 407
2

Floyd's triangle

Read n and print Floyd's triangle (numbers separated by single spaces). For n = 4:

1
2 3
4 5 6
7 8 9 10
Test input:
5
Expected output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Quick Quiz

Q1.

Which loop is best when you know the exact number of repetitions?

Q2.

What is the minimum number of times a do-while body runs?

Q3.

What does break outer; do in nested loops?

Q4.

for (int x : arr) is called...