Loops in Java for ICSE Class 9: for, while, do-while and Nested Loops
ICSE Class 9 Computer Applications notes on loops in Java: for, while and do-while loops, entry vs exit control, break and continue, nested loops and pattern programs, with dry runs, output questions and solved programs.

Loops let a program repeat a set of statements without writing them again and again. Every number program, series and pattern in ICSE Computer Applications uses loops, and output questions on loops appear in almost every exam. These notes take you through all three loops, nested loops and the common exam traps.
Parts of a loop
Every loop has four parts:
- Initialisation: sets the starting value of the loop variable, for example
int i = 1. - Test condition: decides whether the loop body runs again, for example
i <= 10. - Body: the statements that are repeated.
- Update: changes the loop variable, for example
i++.
The for loop
Use a for loop when you know how many times to repeat.
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
// Output: 1 2 3 4 5Sum of the first n natural numbers:
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;The while loop
Use a while loop when the number of repetitions is not known in advance. A typical example is processing the digits of a number.
int n = 4721, sum = 0;
while (n > 0) {
sum += n % 10; // take the last digit
n /= 10; // remove the last digit
}
System.out.println(sum); // 14The do-while loop
The condition is checked after the body, so a do-while loop always runs at least once.
int i = 10;
do {
System.out.println(i);
i++;
} while (i < 5);
// Prints 10 once, even though the condition is falseEntry-controlled vs exit-controlled
- Entry-controlled loops (
forandwhile) check the condition before the body, so the body may run zero times. - Exit-controlled loops (
do-while) check the condition after the body, so it always runs at least once.
break and continue
- break ends the loop immediately.
- continue skips the rest of the current iteration and moves on to the next one.
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0)
continue; // skip multiples of 3
if (i > 7)
break; // stop after 7
System.out.print(i + " ");
}
// Output: 1 2 4 5 7Infinite and empty loops
- Infinite loop: the condition never becomes false. For example,
for (int i = 1; i > 0; i++)keeps running untilioverflows, andwhile (true)runs for ever unless it reaches abreak. - Empty loop: a loop with a semicolon straight after the heading, such as
for (i = 0; i < 5; i++);. The body is empty, and the statement after it runs once.
Nested loops
A loop inside another loop is a nested loop. For each iteration of the outer loop, the inner loop runs completely. Nested loops are used for patterns and tables.
for (int i = 1; i <= 4; i++) { // rows
for (int j = 1; j <= i; j++) // columns
System.out.print("* ");
System.out.println();
}Output:
*
* *
* * *
* * * *Number triangle:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}Output:
1
1 2
1 2 3
1 2 3 4Output questions: practise these
Q1. How many times does this loop run, and what is printed?
int x = 1;
while (x < 50)
x = x * 3;
System.out.println(x);Answer: x takes the values 1, 3, 9, 27 and 81. The loop runs 4 times and prints 81.
Q2. What is printed?
int k;
for (k = 10; k > 0; k -= 4);
System.out.println(k);Answer: -2. The semicolon makes the loop body empty: k goes 10, 6, 2, −2, and then the loop stops. The print statement runs once, after the loop.
Solved program: reverse a number and check for a palindrome
import java.util.Scanner;
class Palindrome {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int t = n, rev = 0;
while (t > 0) {
rev = rev * 10 + t % 10;
t /= 10;
}
if (rev == n)
System.out.println(n + " is a palindrome");
else
System.out.println(n + " is not a palindrome");
}
}Sample run: 1221 gives "1221 is a palindrome".
Exam tips
- When the question says "using a while loop", do not use a
forloop. You lose marks even if the output is correct. - Always copy the number into a temporary variable (
t = n) before breaking it into digits, so the original is still available for the comparison. - Use dry-run tables for every output question.
Continue with Math library methods and practise loops in our ICSE Class 9 solved programs.
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.