Loops in Java
Java Master Course — Chapter 7 of 50
Loops allow a Java program to repeat a block of code. They are one of the most important concepts in programming because many problems require the same operation to be performed multiple times.
1. What Is a Loop?#
Suppose you want to print:
Java
Java
Java
Java
Java
Without a loop, you could write:
System.out.println("Java");
System.out.println("Java");
System.out.println("Java");
System.out.println("Java");
System.out.println("Java");
This works, but it is repetitive.
With a loop:
for (int i = 1; i <= 5; i++) {
System.out.println("Java");
}
Much better.
The basic idea is:
Start
↓
Check condition
↓
┌───┴───┐
true false
│ │
↓ ↓
Execute Stop
body
│
↓
Update
│
└──────► Check again
A loop repeatedly executes code while a condition remains true.
2. Why Do We Need Loops?#
Loops are useful whenever the same operation must happen repeatedly.
Examples:
Print numbers 1 to 100
Read 10 values
Search through an array
Calculate a sum
Process every student
Repeat a menu
Validate input
Generate patterns
Run a game loop
Process records
Without loops, many programs would contain thousands of repeated statements.
3. Real-Life Example#
Imagine you need to water 100 plants.
You could think:
Water plant 1
Water plant 2
Water plant 3
...
Water plant 100
A programmer thinks:
Repeat watering until all plants are done.
That is the basic idea behind loops.
4. Main Loop Types in Java#
Java provides several looping mechanisms.
The most important are:
while
do-while
for
enhanced for
There are also loop-control statements:
break
continue
You can also create nested loops.
We will learn all of them.
5. while Loop#
The while loop is the simplest condition-controlled loop.
Syntax:
while (condition) {
// repeated code
}
Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Output:
1
2
3
4
5
6. How while Works#
Consider:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Execution:
i = 1
Check: 1 <= 5 → true
Print 1
i becomes 2
Check: 2 <= 5 → true
Print 2
i becomes 3
Check: 3 <= 5 → true
Print 3
i becomes 4
Check: 4 <= 5 → true
Print 4
i becomes 5
Check: 5 <= 5 → true
Print 5
i becomes 6
Check: 6 <= 5 → false
Stop
The condition is checked before every iteration.
7. Important Parts of a Loop#
Most counter-based loops have three important components:
Initialization
↓
Condition
↓
Update
Example:
int i = 1; // initialization
while (i <= 5) { // condition
System.out.println(i);
i++; // update
}
Think:
Start
↓
Where do I begin?
↓
Should I continue?
↓
Do the work
↓
Change the counter
↓
Check again
8. Initialization#
Initialization gives the loop its starting state.
Example:
int i = 1;
Other examples:
int i = 0;
int count = 10;
int sum = 0;
Initialization normally happens before the loop begins.
9. Condition#
The condition determines whether another iteration should happen.
Example:
i <= 10
As long as it is:
true
the loop continues.
When it becomes:
false
the loop stops.
10. Update#
The update changes the loop state.
Example:
i++;
Without an update, a counter loop may never terminate.
Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
}
This can run forever because i never changes.
11. Infinite Loop#
An infinite loop never reaches a false condition.
Example:
while (true) {
System.out.println("Hello");
}
This continues indefinitely unless something externally stops the program or the loop exits.
Another example:
int i = 1;
while (i <= 5) {
System.out.println(i);
}
The condition remains:
1 <= 5
forever.
12. Intentional Infinite Loops#
Infinite loops are not always mistakes.
They can be intentional.
Example:
while (true) {
// repeatedly process application events
}
Usually there will be some internal exit condition:
while (true) {
System.out.print("Enter command: ");
String command = sc.nextLine();
if (command.equals("exit")) {
break;
}
}
This creates a loop that continues until the user enters exit.
13. Counter-Controlled Loop#
A common pattern is:
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
Here:
initial value = 1
final condition = <= 10
step = +1
This is called a counter-controlled loop.
14. Counting Backwards#
You can decrement instead.
int i = 5;
while (i >= 1) {
System.out.println(i);
i--;
}
Output:
5
4
3
2
1
15. Different Step Sizes#
You do not have to increase by exactly 1.
Example:
int i = 0;
while (i <= 20) {
System.out.println(i);
i += 2;
}
Output:
0
2
4
6
8
10
12
14
16
18
20
You can use:
i += 5;
or:
i -= 2;
depending on the problem.
16. do-while Loop#
The do-while loop is similar to while, but with one important difference:
The body executes at least once.
Syntax:
do {
// code
} while (condition);
Example:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
1
2
3
4
5
17. while vs do-while#
while:
while (condition) {
// body
}
The condition is checked first.
do-while:
do {
// body
} while (condition);
The body runs first, then the condition is checked.
18. Important Difference#
Consider:
int i = 10;
while (i < 5) {
System.out.println("Hello");
}
Output:
The body never runs.
Now:
int i = 10;
do {
System.out.println("Hello");
} while (i < 5);
Output:
Hello
Even though:
10 < 5
is false.
Why?
Because do-while executes its body before checking the condition.
19. When Is do-while Useful?#
It is useful when an operation must happen at least once.
A classic example is a menu:
Show menu
↓
Read choice
↓
Process choice
↓
Ask whether to continue
The menu needs to appear at least once.
20. Menu Example with do-while#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println();
System.out.println("===== MENU =====");
System.out.println("1. Say Hello");
System.out.println("2. Say Bye");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> System.out.println("Hello!");
case 2 -> System.out.println("Bye!");
case 3 -> System.out.println("Exiting...");
default -> System.out.println("Invalid choice");
}
} while (choice != 3);
sc.close();
}
}
The menu appears at least once.
21. for Loop#
The for loop is commonly used when you know or can clearly express the loop's initialization, condition, and update.
Syntax:
for (initialization; condition; update) {
// body
}
Example:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output:
1
2
3
4
5
22. Anatomy of a for Loop#
Consider:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Break it down:
for (
int i = 1; → initialization
i <= 5; → condition
i++ → update
)
The body:
System.out.println(i);
runs on each iteration.
23. Exact Execution Order of for#
Example:
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
Execution:
1. int i = 1
2. i <= 3 → true
3. print 1
4. i++
5. i <= 3 → true
6. print 2
7. i++
8. i <= 3 → true
9. print 3
10. i++
11. i <= 3 → false
12. stop
Important:
Initialization happens once.
Condition is checked before each iteration.
Update happens after the body.
24. for Loop vs while Loop#
These are equivalent:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
and:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
The main difference is organization.
Use for when the initialization, condition, and update naturally belong together.
Use while when the stopping condition is more naturally expressed independently.
25. Counting from 0#
Many programming tasks start at zero.
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Output:
0
1
2
3
4
Notice:
i < 5
not:
i <= 5
This produces exactly five iterations.
26. Number of Iterations#
For:
for (int i = 0; i < 5; i++) {
}
values are:
0
1
2
3
4
Total:
5 iterations
For:
for (int i = 1; i <= 5; i++) {
}
values are:
1
2
3
4
5
Total:
5 iterations
27. < vs <=#
This is one of the most common loop mistakes.
for (int i = 0; i < 5; i++)
means:
0 to 4
while:
for (int i = 0; i <= 5; i++)
means:
0 to 5
which is six iterations.
Always check whether the endpoint should be included.
28. Counting Down with for#
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
Output:
5
4
3
2
1
29. Counting by 2#
for (int i = 2; i <= 20; i += 2) {
System.out.println(i);
}
Output:
2
4
6
8
10
12
14
16
18
20
30. Reverse Counting by 2#
for (int i = 20; i >= 2; i -= 2) {
System.out.println(i);
}
Output:
20
18
16
14
12
10
8
6
4
2
31. Multiple Variables in a for Loop#
A for loop can initialize multiple variables.
Example:
for (int i = 1, j = 10; i <= 5; i++, j--) {
System.out.println(i + " " + j);
}
Output:
1 10
2 9
3 8
4 7
5 6
Both counters are updated each iteration.
32. Omitting Parts of for#
Java allows parts of the for header to be omitted.
For example:
int i = 1;
for (; i <= 5; i++) {
System.out.println(i);
}
Initialization was moved outside.
You can also write:
for (int i = 1; ; i++) {
if (i > 5) {
break;
}
System.out.println(i);
}
The condition is omitted, creating an infinite loop unless break exits it.
This is legal, but use it only when it improves clarity.
33. Infinite for Loop#
This:
for (;;) {
System.out.println("Hello");
}
is an infinite loop.
It is equivalent in spirit to:
while (true) {
System.out.println("Hello");
}
Usually you will see this pattern with an explicit exit condition:
for (;;) {
// work
if (shouldStop) {
break;
}
}
34. Scope of the for Variable#
Consider:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
The variable:
i
exists within the scope of the for statement and its body.
This is not valid:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
System.out.println(i);
because i is out of scope after the loop.
35. Declaring the Counter Outside#
If you need the counter afterward:
int i;
for (i = 0; i < 5; i++) {
System.out.println(i);
}
System.out.println("After loop: " + i);
Output:
0
1
2
3
4
After loop: 5
36. Nested Loops#
A loop inside another loop is called a:
nested loop
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
}
}
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
37. How Nested Loops Work#
The outer loop controls:
i
The inner loop completes all of its iterations for every outer iteration.
Think:
i = 1
j = 1
j = 2
j = 3
i = 2
j = 1
j = 2
j = 3
i = 3
j = 1
j = 2
j = 3
If the outer loop runs 3 times and the inner loop runs 3 times each time:
3 × 3 = 9
inner iterations occur.
38. Nested Loop — Multiplication Table#
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print((i * j) + "\t");
}
System.out.println();
}
This produces a multiplication table.
39. Nested Loop — Rectangle Pattern#
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= 5; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
The outer loop controls rows.
The inner loop controls columns.
40. Nested Loop — Triangle Pattern#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
*
* *
* * *
* * * *
* * * * *
Notice that the number of inner iterations depends on the outer loop variable.
41. Nested Loop — Number Triangle#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(col + " ");
}
System.out.println();
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
42. Nested Loop — Same Number Pattern#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(row + " ");
}
System.out.println();
}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
43. Loop Control — break#
break immediately exits the nearest loop.
Example:
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
Output:
1
2
3
4
When:
i == 5
becomes true, the loop stops.
44. Flow of break#
Loop starts
↓
Iteration
↓
break condition?
↓
yes ───────► Exit loop
│
no
↓
Continue
break exits the nearest enclosing loop or switch statement.
45. break in while#
int i = 1;
while (i <= 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
Output:
1
2
3
4
46. break in Nested Loops#
Consider:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break;
}
System.out.println(i + " " + j);
}
}
Output:
1 1
2 1
3 1
Why?
break exits only the nearest loop:
inner loop
It does not automatically stop the outer loop.
47. Labeled break#
Java allows labeled statements.
Example:
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break outer;
}
System.out.println(i + " " + j);
}
}
Output:
1 1
1 2
1 3
2 1
When:
i = 2
j = 2
the labeled break exits the outer loop.
Labeled breaks are legal but should be used carefully because simpler control flow is often easier to understand.
48. continue#
continue skips the remaining body of the current iteration and moves to the next iteration.
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
The iteration where i == 3 is skipped.
49. break vs continue#
This difference is extremely important.
break
↓
stop the loop completely
continue
↓
skip current iteration
then continue loop
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output:
1
2
But:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
50. continue in a while Loop#
Be careful with continue in a while loop.
Bad:
int i = 1;
while (i <= 5) {
if (i == 3) {
continue;
}
System.out.println(i);
i++;
}
When i becomes 3, continue jumps back to the condition without executing:
i++;
So i remains 3 forever.
This creates an infinite loop.
Correct:
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
System.out.println(i);
i++;
}
Or structure the loop differently.
51. Enhanced for Loop#
Java provides an enhanced for loop, also called the:
for-each loop
It is especially useful for arrays and collections.
Syntax:
for (Type variable : collectionOrArray) {
// use variable
}
Example:
int[] numbers = {10, 20, 30, 40};
for (int number : numbers) {
System.out.println(number);
}
Output:
10
20
30
40
52. Why Use Enhanced for?#
Suppose you only want to process every element of an array.
Traditional loop:
int[] numbers = {10, 20, 30, 40};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Enhanced loop:
for (int number : numbers) {
System.out.println(number);
}
The second version is simpler when you don't need the index.
53. Understanding the Enhanced for#
For:
for (int number : numbers) {
System.out.println(number);
}
read it as:
For each int number in numbers:
print number
If:
numbers = [10, 20, 30, 40]
iterations are:
number = 10
number = 20
number = 30
number = 40
54. Enhanced for with Strings#
String[] languages = {
"Java",
"Python",
"C++"
};
for (String language : languages) {
System.out.println(language);
}
Output:
Java
Python
C++
55. Enhanced for with char[]#
char[] letters = {'J', 'a', 'v', 'a'};
for (char ch : letters) {
System.out.println(ch);
}
Output:
J
a
v
a
56. Enhanced for and Index#
The enhanced for loop does not directly provide the current index.
If you need the index, use a traditional loop:
for (int i = 0; i < numbers.length; i++) {
System.out.println(i + ": " + numbers[i]);
}
If you only need the values:
for (int number : numbers) {
System.out.println(number);
}
57. Enhanced for and Modification#
Suppose:
int[] numbers = {1, 2, 3};
for (int number : numbers) {
number = number * 2;
}
This does not modify the array elements.
Why?
number is a local loop variable holding the current element value.
The array remains:
1 2 3
To modify elements, use an index:
for (int i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}
Now the array becomes:
2 4 6
58. Enhanced for with Object References#
Later, when you study OOP, you will see:
Student[] students = ...;
for (Student student : students) {
student.display();
}
The loop variable contains a reference to each object.
This becomes extremely useful when processing collections of objects.
59. Looping Through a String#
A String is not directly an array, but you can access its characters.
Example:
String text = "Java";
for (int i = 0; i < text.length(); i++) {
System.out.println(text.charAt(i));
}
Output:
J
a
v
a
60. Counting Characters#
String text = "banana";
int count = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == 'a') {
count++;
}
}
System.out.println("a count = " + count);
Output:
a count = 3
This demonstrates:
loop
+
condition
+
counter
61. Sum of Numbers#
Calculate:
1 + 2 + 3 + ... + 10
Code:
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println(sum);
Output:
55
62. Understanding the Sum#
Initially:
sum = 0
Iteration:
i = 1 → sum = 1
i = 2 → sum = 3
i = 3 → sum = 6
i = 4 → sum = 10
...
i = 10 → sum = 55
This is called an accumulator pattern.
63. Accumulator Pattern#
A common loop structure is:
result = initialValue;
for (...) {
result = result + currentValue;
}
Examples:
sum = sum + number;
or:
product = product * number;
or:
total += price;
This pattern appears everywhere in programming.
64. Product of Numbers#
Calculate:
1 × 2 × 3 × 4 × 5
Code:
int product = 1;
for (int i = 1; i <= 5; i++) {
product *= i;
}
System.out.println(product);
Output:
120
This is also the basis of factorial.
65. Factorial#
Mathematically:
5! = 5 × 4 × 3 × 2 × 1
= 120
Java:
int n = 5;
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println(factorial);
Output:
120
For large values, even long can overflow. Later chapters will discuss numeric limits and safer approaches.
66. Factorial with User Input#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
long factorial = 1;
if (n < 0) {
System.out.println("Factorial is not defined for negative integers.");
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println("Factorial = " + factorial);
}
sc.close();
}
}
67. Multiplication Table#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(n + " × " + i + " = " + (n * i));
}
sc.close();
}
}
Input:
5
Output:
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50
68. Counting Digits#
Suppose:
number = 12345
We want:
5 digits
One common approach:
int number = 12345;
int count = 0;
while (number != 0) {
number /= 10;
count++;
}
System.out.println(count);
Output:
5
69. How Digit Counting Works#
Start:
12345
Divide by 10:
1234
Again:
123
Again:
12
Again:
1
Again:
0
Number of divisions:
5
Therefore:
5 digits
70. Special Case — Zero#
The previous method gives:
0 digits
for:
number = 0;
But zero has one digit.
Handle it separately:
int number = 0;
int count;
if (number == 0) {
count = 1;
} else {
count = 0;
while (number != 0) {
number /= 10;
count++;
}
}
System.out.println(count);
71. Reverse a Number#
Example:
Input: 1234
Output: 4321
Code:
int number = 1234;
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
System.out.println(reverse);
Output:
4321
72. Reverse Number — Step by Step#
Start:
number = 1234
reverse = 0
First digit:
1234 % 10 = 4
Then:
reverse = 0 × 10 + 4
= 4
Remove digit:
1234 / 10 = 123
Next:
123 % 10 = 3
reverse = 4 × 10 + 3
= 43
Next:
2
reverse = 432
Next:
1
reverse = 4321
Finally:
number = 0
Loop stops.
73. Palindrome Number#
A palindrome reads the same forward and backward.
Examples:
121
1331
1221
Not palindromes:
123
1234
We can reverse the number and compare.
int number = 121;
int original = number;
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
if (original == reverse) {
System.out.println("Palindrome");
} else {
System.out.println("Not palindrome");
}
Output:
Palindrome
74. Sum of Digits#
For:
1234
sum:
1 + 2 + 3 + 4 = 10
Program:
int number = 1234;
int sum = 0;
while (number != 0) {
int digit = number % 10;
sum += digit;
number /= 10;
}
System.out.println(sum);
Output:
10
75. Count Even Numbers#
int[] numbers = {10, 15, 20, 25, 30};
int count = 0;
for (int number : numbers) {
if (number % 2 == 0) {
count++;
}
}
System.out.println("Even numbers = " + count);
Output:
Even numbers = 3
76. Find Maximum#
int[] numbers = {10, 50, 20, 90, 30};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("Maximum = " + max);
Output:
Maximum = 90
This is a very important algorithmic pattern.
77. Find Minimum#
int[] numbers = {10, 50, 20, 90, 30};
int min = numbers[0];
for (int number : numbers) {
if (number < min) {
min = number;
}
}
System.out.println("Minimum = " + min);
Output:
Minimum = 10
78. Search for a Value#
int[] numbers = {10, 20, 30, 40, 50};
int target = 30;
boolean found = false;
for (int number : numbers) {
if (number == target) {
found = true;
break;
}
}
if (found) {
System.out.println("Found");
} else {
System.out.println("Not found");
}
Output:
Found
Notice how break avoids unnecessary additional searching after the target is found.
79. Linear Search#
The previous example is a basic:
linear search
The algorithm checks elements one by one.
For:
[10, 20, 30, 40, 50]
searching for 40:
10 → no
20 → no
30 → no
40 → yes
Then we can stop.
This is an important foundation for later data structures and algorithms.
80. Loop with Input#
You can use loops to process repeated user input.
Example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter number " + i + ": ");
int number = sc.nextInt();
sum += number;
}
System.out.println("Sum = " + sum);
sc.close();
}
}
Example:
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
Sum = 150
81. Sentinel-Controlled Loop#
Sometimes you do not know how many values the user will enter.
You can use a special value called a:
sentinel
Example:
Enter numbers.
Enter -1 to stop.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
while (true) {
System.out.print("Enter number (-1 to stop): ");
int number = sc.nextInt();
if (number == -1) {
break;
}
sum += number;
}
System.out.println("Sum = " + sum);
sc.close();
}
}
The sentinel is:
-1
82. Validation Loop#
Loops are excellent for repeatedly asking for valid input.
Example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age;
while (true) {
System.out.print("Enter age between 1 and 120: ");
age = sc.nextInt();
if (age >= 1 && age <= 120) {
break;
}
System.out.println("Invalid age. Try again.");
}
System.out.println("Accepted age = " + age);
sc.close();
}
}
This pattern is used constantly in real applications.
83. while for Unknown Number of Iterations#
Use while when you don't know exactly how many times the loop will run.
Example:
while (userHasNotExited) {
// process command
}
The loop may run:
1 time
10 times
100 times
0 times
depending on the input.
84. for for Known Iterations#
If you know the number of repetitions:
for (int i = 0; i < 10; i++) {
...
}
This clearly expresses:
repeat 10 times
This is one reason for is so common.
85. do-while for At-Least-Once Logic#
If the body must execute at least once:
do {
...
} while (...);
Examples:
menus
user prompts
retry operations
86. Choosing the Right Loop#
A useful guideline:
Known/countable repetitions
↓
for
Condition-controlled repetition
↓
while
Must execute at least once
↓
do-while
Process every element of array/collection
↓
enhanced for
These are guidelines, not strict rules.
87. Nested Loops and Complexity#
Suppose:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// work
}
}
The inner body executes approximately:
n × n
times:
n²
This leads to the concept of algorithmic complexity.
For example:
n = 10
→ 100 iterations
n = 100
→ 10,000 iterations
n = 1000
→ 1,000,000 iterations
You will study algorithm analysis more deeply in data structures and algorithms.
88. Three Nested Loops#
You can nest more than two loops.
Example:
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 4; k++) {
System.out.println(i + " " + j + " " + k);
}
}
}
Total inner executions:
2 × 3 × 4 = 24
Nested loops can become expensive quickly.
89. Avoid Unnecessary Nested Loops#
Sometimes a nested loop is required.
But do not use one automatically.
For example, searching for a value in an array can often use one loop:
for (int number : numbers) {
if (number == target) {
...
}
}
A second loop may be unnecessary.
Good programming is not just about making code work; it is also about choosing an appropriate algorithm.
90. Loop Invariants — Basic Idea#
A useful algorithmic concept is a:
loop invariant
It is something that remains true at a particular point during every iteration.
For example, in:
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
After each iteration, sum represents the sum of all integers processed so far.
Understanding invariants helps you prove that loops are correct.
You do not need advanced mathematical proof yet, but learning to ask:
What does my variable mean after every iteration?
is an excellent habit.
91. Dry Run a Loop#
A dry run means manually tracing the values.
Example:
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
Trace:
| Iteration | i |
sum |
|---|---|---|
| Start | — | 0 |
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
Final:
sum = 10
Dry runs are extremely useful when learning loops.
92. Common Loop Mistake — Off-by-One#
Consider:
for (int i = 0; i <= 10; i++) {
System.out.println(i);
}
This prints:
0 through 10
That is:
11 values
If you wanted exactly 10 values:
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
prints:
0 through 9
This is called an:
off-by-one error
93. Common Loop Mistake — Wrong Update Direction#
This is dangerous:
for (int i = 10; i >= 1; i++) {
System.out.println(i);
}
The condition requires i to decrease, but the code increases it.
Correct:
for (int i = 10; i >= 1; i--) {
System.out.println(i);
}
Always make sure the update moves toward the stopping condition.
94. Common Loop Mistake — Forgetting Update#
Bad:
int i = 1;
while (i <= 10) {
System.out.println(i);
}
i never changes.
Correct:
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
95. Common Loop Mistake — Updating the Wrong Variable#
Consider:
int i = 1;
int j = 10;
while (i <= 5) {
System.out.println(i);
j++;
}
The condition depends on:
i
but the code updates:
j
Therefore i never changes and the loop does not terminate normally.
96. Common Loop Mistake — Accumulator Initialization#
For a sum:
int sum = 0;
is normally appropriate.
For a product:
int product = 1;
is normally appropriate.
Why not:
int product = 0;
Because:
0 × anything = 0
The accumulator's initial value must match the operation.
97. Common Loop Mistake — Modifying the Loop Counter Inside the Body#
Example:
for (int i = 0; i < 10; i++) {
i++;
System.out.println(i);
}
This is legal but makes the number of iterations less obvious.
It can be appropriate in special cases, but beginners should generally keep the counter update in one clear place.
Prefer:
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
when no special behavior is needed.
98. Common Loop Mistake — Floating-Point Loop Counters#
Avoid using floating-point values as loop counters when possible.
For example:
for (double x = 0; x <= 1; x += 0.1) {
...
}
Floating-point representation can cause surprising boundary behavior.
Prefer integer counters:
for (int i = 0; i <= 10; i++) {
double x = i / 10.0;
}
This gives more predictable control.
99. break vs Boolean Flag#
Instead of:
while (true) {
...
if (done) {
break;
}
}
you can sometimes use:
while (!done) {
...
}
Both can be valid.
Use the form that makes the termination logic easiest to understand.
100. continue and Readability#
Although continue can be useful, too many early jumps can make a loop difficult to follow.
Example:
for (int number : numbers) {
if (number < 0) {
continue;
}
if (number == 0) {
continue;
}
...
}
Sometimes this is clearer as:
for (int number : numbers) {
if (number > 0) {
...
}
}
Do not avoid continue completely; just use it when it makes the logic clearer.
101. Looping Through Arrays#
Arrays will be covered in detail in Chapter 9, but loops are essential for using arrays.
Example:
int[] marks = {80, 90, 70, 85};
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
Output:
80
90
70
85
Notice:
marks.length
gives the number of elements.
102. Enhanced Loop with Array#
The same task can be written:
int[] marks = {80, 90, 70, 85};
for (int mark : marks) {
System.out.println(mark);
}
This is often cleaner when the index is unnecessary.
103. Sum of Array Elements#
int[] numbers = {10, 20, 30, 40};
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println("Sum = " + sum);
Output:
Sum = 100
104. Average of Array Elements#
int[] numbers = {10, 20, 30, 40};
int sum = 0;
for (int number : numbers) {
sum += number;
}
double average = (double) sum / numbers.length;
System.out.println("Average = " + average);
Output:
Average = 25.0
The cast:
(double) sum
ensures floating-point division.
105. Count Values Above a Limit#
int[] marks = {45, 80, 90, 30, 70};
int count = 0;
for (int mark : marks) {
if (mark >= 50) {
count++;
}
}
System.out.println("Passing students = " + count);
Output:
Passing students = 3
106. Search and Stop#
int[] numbers = {10, 20, 30, 40, 50};
int target = 40;
for (int number : numbers) {
if (number == target) {
System.out.println("Found");
break;
}
}
The break prevents unnecessary work after the value is found.
107. Loop Through a 2D Array#
Two-dimensional arrays are covered later, but the loop pattern is important.
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
Output:
1 2 3
4 5 6
108. Nested Enhanced for#
You can also use enhanced loops:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
Output:
1 2 3
4 5 6
109. Looping Through a String — Reverse#
String text = "Java";
for (int i = text.length() - 1; i >= 0; i--) {
System.out.print(text.charAt(i));
}
Output:
avaJ
110. Count Vowels#
String text = "programming";
int count = 0;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u') {
count++;
}
}
System.out.println("Vowels = " + count);
Output:
Vowels = 3
This is a good example of:
String
+
loop
+
condition
+
counter
111. Find First Matching Character#
String text = "programming";
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == 'g') {
System.out.println("First g at index " + i);
break;
}
}
Output:
First g at index 3
112. Print Only Odd Numbers#
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
Output:
1
3
5
7
9
11
13
15
17
19
Here continue skips even numbers.
113. Print Multiples#
for (int i = 1; i <= 50; i++) {
if (i % 5 == 0) {
System.out.println(i);
}
}
Output:
5
10
15
20
25
30
35
40
45
50
114. Prime Number Check#
A prime number has exactly two positive divisors:
1
itself
Examples:
2
3
5
7
11
13
A simple approach:
int n = 29;
boolean prime = true;
if (n < 2) {
prime = false;
} else {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
}
if (prime) {
System.out.println("Prime");
} else {
System.out.println("Not prime");
}
Output:
Prime
115. Better Prime Check#
You don't need to test all numbers below n.
If a number has a divisor larger than its square root, it must also have a corresponding divisor smaller than the square root.
So you can test:
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
Complete example:
int n = 29;
boolean prime = n >= 2;
for (int i = 2; i * i <= n && prime; i++) {
if (n % i == 0) {
prime = false;
}
}
System.out.println(prime ? "Prime" : "Not prime");
This is more efficient than checking every number below n.
116. Print Prime Numbers#
for (int n = 2; n <= 50; n++) {
boolean prime = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (prime) {
System.out.print(n + " ");
}
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
This demonstrates nested loops.
117. Fibonacci Sequence#
A common beginner problem is Fibonacci numbers:
0 1 1 2 3 5 8 13 21 ...
Each number is generally the sum of the previous two.
Program:
int n = 10;
int a = 0;
int b = 1;
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
Output:
0 1 1 2 3 5 8 13 21 34
118. GCD Using a Loop#
A simple method for finding the greatest common divisor:
int a = 48;
int b = 18;
int gcd = 1;
for (int i = 1; i <= Math.min(a, b); i++) {
if (a % i == 0 && b % i == 0) {
gcd = i;
}
}
System.out.println("GCD = " + gcd);
Output:
GCD = 6
There are more efficient algorithms, such as Euclid's algorithm, which you may encounter later.
119. Practical Program — Sum Until Zero#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
while (true) {
System.out.print("Enter number (0 to stop): ");
int n = sc.nextInt();
if (n == 0) {
break;
}
sum += n;
}
System.out.println("Sum = " + sum);
sc.close();
}
}
120. Practical Program — Count Positive and Negative Values#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int positive = 0;
int negative = 0;
int zero = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter number " + i + ": ");
int n = sc.nextInt();
if (n > 0) {
positive++;
} else if (n < 0) {
negative++;
} else {
zero++;
}
}
System.out.println("Positive: " + positive);
System.out.println("Negative: " + negative);
System.out.println("Zero: " + zero);
sc.close();
}
}
121. Practical Program — Find Largest Input#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers? ");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("Number of values must be positive.");
sc.close();
return;
}
System.out.print("Enter number 1: ");
int max = sc.nextInt();
for (int i = 2; i <= n; i++) {
System.out.print("Enter number " + i + ": ");
int value = sc.nextInt();
if (value > max) {
max = value;
}
}
System.out.println("Maximum = " + max);
sc.close();
}
}
122. Practical Program — Guessing Game#
This is a simple loop-based game.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int secret = 42;
int guess;
do {
System.out.print("Guess the number: ");
guess = sc.nextInt();
if (guess < secret) {
System.out.println("Too low");
} else if (guess > secret) {
System.out.println("Too high");
} else {
System.out.println("Correct!");
}
} while (guess != secret);
sc.close();
}
}
This combines:
do-while
if-else
input
comparison
123. Practical Program — Menu Repetition#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println();
System.out.println("1. Add");
System.out.println("2. Multiply");
System.out.println("3. Exit");
System.out.print("Choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Addition selected");
break;
case 2:
System.out.println("Multiplication selected");
break;
case 3:
System.out.println("Goodbye");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 3);
sc.close();
}
}
This pattern is the foundation of many command-line applications.
124. Practical Program — ATM Loop#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double balance = 10000;
int choice;
do {
System.out.println();
System.out.println("===== ATM =====");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.printf("Balance = %.2f%n", balance);
break;
case 2:
System.out.print("Enter deposit amount: ");
double deposit = sc.nextDouble();
if (deposit > 0) {
balance += deposit;
System.out.println("Deposit successful");
} else {
System.out.println("Invalid amount");
}
break;
case 3:
System.out.print("Enter withdrawal amount: ");
double withdrawal = sc.nextDouble();
if (withdrawal <= 0) {
System.out.println("Invalid amount");
} else if (withdrawal > balance) {
System.out.println("Insufficient balance");
} else {
balance -= withdrawal;
System.out.println("Withdrawal successful");
}
break;
case 4:
System.out.println("Thank you");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 4);
sc.close();
}
}
This combines many concepts learned so far.
125. Loop Patterns#
Pattern problems are useful for learning nested loops.
The general structure is:
for (int row = ...; ...; ...) {
for (int col = ...; ...; ...) {
// print something
}
System.out.println();
}
Think:
outer loop
→ rows
inner loop
→ columns
126. Square Pattern#
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= 4; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * *
* * * *
* * * *
* * * *
127. Increasing Triangle#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
*
* *
* * *
* * * *
* * * * *
128. Decreasing Triangle#
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * * *
* * * *
* * *
* *
*
129. Right-Aligned Triangle#
int n = 5;
for (int row = 1; row <= n; row++) {
for (int space = 1; space <= n - row; space++) {
System.out.print(" ");
}
for (int star = 1; star <= row; star++) {
System.out.print("* ");
}
System.out.println();
}
Output:
*
* *
* * *
* * * *
* * * * *
This teaches you to use multiple inner loops for one row.
130. Pyramid Pattern#
int n = 5;
for (int row = 1; row <= n; row++) {
for (int space = 1; space <= n - row; space++) {
System.out.print(" ");
}
for (int star = 1; star <= 2 * row - 1; star++) {
System.out.print("*");
}
System.out.println();
}
Output:
*
***
*****
*******
*********
The formula:
2 * row - 1
produces:
1
3
5
7
9
stars.
131. Number Pattern#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(col);
}
System.out.println();
}
Output:
1
12
123
1234
12345
132. Repeated Number Pattern#
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(row);
}
System.out.println();
}
Output:
1
22
333
4444
55555
133. break and continue in Nested Loops#
Remember:
break;
affects the nearest enclosing loop.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break;
}
System.out.println(i + "," + j);
}
}
Only the inner loop stops each time.
For the outer loop too, use a labeled break if appropriate:
outer:
for (...) {
for (...) {
if (...) {
break outer;
}
}
}
134. continue in Nested Loops#
A continue applies to the nearest loop.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
System.out.println(i + "," + j);
}
}
For every row, the j == 2 iteration is skipped.
135. Labeled continue#
Java also supports labeled continue.
Example:
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + "," + j);
}
}
When j == 2, the program skips to the next iteration of the outer loop.
This is advanced control flow. Use it only when it makes the algorithm clearer.
136. Loop and Method Interaction#
A loop can call a method.
Example:
public class Main {
static void printSquare(int n) {
System.out.println(n * n);
}
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
printSquare(i);
}
}
}
Output:
1
4
9
16
25
Methods are covered deeply in Chapter 8.
137. Loop and return#
return exits the current method, not just the loop.
Example:
public class Main {
static void findFirst(int[] numbers, int target) {
for (int number : numbers) {
if (number == target) {
System.out.println("Found");
return;
}
}
System.out.println("Not found");
}
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
findFirst(numbers, 20);
}
}
Output:
Found
Once return executes, the method ends.
138. break vs return#
break
→ exits loop
return
→ exits method
Example:
for (...) {
if (...) {
break;
}
}
The method continues after the loop.
But:
for (...) {
if (...) {
return;
}
}
the current method ends.
139. Loop and Exceptions#
You may eventually have code like:
try {
// operation
} catch (...) {
// handle error
}
inside a loop.
Or a loop may continue after handling an invalid input.
Exception handling is covered in Chapter 25, so for now focus on the loop mechanics.
140. Loop Performance Basics#
For simple loops:
for (int i = 0; i < n; i++) {
...
}
the body runs approximately:
n times
Nested:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
...
}
}
approximately:
n² times
Three levels:
n³
Understanding this helps you write efficient programs.
141. Avoid Repeating Expensive Work#
Suppose:
for (int i = 0; i < array.length; i++) {
expensiveCalculation();
}
If the calculation does not depend on i, doing it every iteration may be wasteful.
Prefer:
var result = expensiveCalculation();
for (int i = 0; i < array.length; i++) {
use(result);
}
The exact optimization depends on the program, but the general principle is:
Do not repeatedly calculate something that does not need to change.
Do not optimize blindly; first make the code correct and readable.
142. Infinite Loop Checklist#
If your program is stuck in a loop, ask:
1. What condition controls the loop?
2. Which variable changes?
3. Does that variable move toward termination?
4. Can the condition ever become false?
5. Did I accidentally use the wrong update?
6. Did continue skip the update?
7. Did I accidentally use <= instead of <?
These questions solve many beginner loop bugs.
143. Loop Debugging Example#
Suppose:
int i = 1;
while (i <= 5) {
System.out.println(i);
i += 2;
}
Trace:
i = 1
print 1
i = 3
print 3
i = 5
print 5
i = 7
7 <= 5 → false
Output:
1
3
5
Dry-running like this makes loop behavior predictable.
144. Another Dry Run#
int x = 0;
for (int i = 1; i <= 3; i++) {
x += i * 2;
}
Trace:
Start x = 0
i = 1
x = 0 + 2
x = 2
i = 2
x = 2 + 4
x = 6
i = 3
x = 6 + 6
x = 12
Final:
x = 12
145. Loop Design Formula#
When creating a loop, ask four questions:
1. What is the starting state?
2. What condition means "continue"?
3. What work happens each iteration?
4. How does the state change?
Example:
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
Answers:
Starting state → i = 1
Continue → i <= 10
Work → print i
Update → i++
If you can answer these four questions, you can design most basic loops.
146. Loop Types Summary#
while#
while (condition) {
...
}
Condition checked first.
May execute zero times.
do-while#
do {
...
} while (condition);
Body executes first.
Executes at least once.
for#
for (initialization; condition; update) {
...
}
Good for counter-controlled loops.
Enhanced for#
for (Type value : arrayOrCollection) {
...
}
Good for processing each element.
147. Comparison Table#
| Loop | Condition checked | Minimum executions | Common use |
|---|---|---|---|
while |
before body | 0 | unknown repetitions |
do-while |
after body | 1 | menu/retry |
for |
before body | 0 | known/countable repetitions |
enhanced for |
internally per element | 0 | arrays/collections |
148. Important Loop Keywords#
break
continue
break:
exit loop
continue:
skip current iteration
return:
exit current method
149. Practice Set — Basic Loops#
Write programs to:
- Print numbers from 1 to 10.
- Print numbers from 10 to 1.
- Print even numbers from 1 to 100.
- Print odd numbers from 1 to 100.
- Print multiples of 5 up to 100.
- Print numbers from 1 to
n. - Print numbers from
nto 1. - Find the sum from 1 to
n. - Find the product from 1 to
n. - Calculate factorial.
- Print a multiplication table.
- Count digits.
- Find sum of digits.
- Reverse a number.
- Check palindrome.
150. Practice Set — Conditions + Loops#
Write programs to:
- Count positive and negative numbers.
- Count even and odd numbers.
- Find the largest number.
- Find the smallest number.
- Check whether a number is prime.
- Print all primes from 1 to 100.
- Print all factors of a number.
- Find GCD.
- Find LCM.
- Check Armstrong number.
- Generate Fibonacci numbers.
- Count vowels in a String.
- Count digits in a String.
- Find the first occurrence of a character.
- Find the number of times a character appears.
151. Practice Set — Nested Loops#
Create:
*
**
***
****
*****
Then:
*****
****
***
**
*
Then:
1
12
123
1234
12345
Then:
1
22
333
4444
55555
Then:
*
***
*****
*******
*********
152. Practice Set — break#
Write programs that:
- Print 1–100 but stop at 50.
- Search for a number in an array and stop when found.
- Ask for numbers until the user enters 0.
- Print numbers until the first negative value.
- Stop a menu when the user chooses Exit.
153. Practice Set — continue#
Write programs that:
- Print 1–100 but skip even numbers.
- Print 1–100 but skip multiples of 5.
- Process only positive numbers.
- Skip zero values.
- Print only numbers divisible by 3.
154. Practice Set — Enhanced for#
Given:
int[] numbers = {10, 20, 30, 40, 50};
Use an enhanced for loop to:
- Print every element.
- Calculate the sum.
- Calculate the average.
- Find the maximum.
- Find the minimum.
- Count even values.
- Count values greater than 25.
- Search for 40.
155. Mini Project — Number Analyzer#
Create a program that repeatedly asks for numbers until the user enters 0.
At the end display:
Count
Sum
Average
Largest
Smallest
Positive count
Negative count
Even count
Odd count
This project combines nearly everything from Chapters 3–7.
156. Mini Project — Multiplication Table Generator#
Ask:
Enter starting number:
Enter ending number:
Then generate tables for every number in that range.
For example:
Start = 2
End = 4
Output:
2 × 1 = 2
...
2 × 10 = 20
3 × 1 = 3
...
3 × 10 = 30
4 × 1 = 4
...
4 × 10 = 40
Use nested loops.
157. Mini Project — Simple Quiz#
Create a quiz with several questions.
For each question:
display question
read answer
check answer
update score
At the end:
Score = X / N
Use loops to avoid repeating the same overall structure manually.
158. Mini Project — ATM#
Improve the ATM program so that:
1 → Balance
2 → Deposit
3 → Withdraw
4 → Exit
continues until the user selects:
4
Use:
do-while
switch
if-else
159. Mini Project — Guessing Game#
Create a number guessing game.
Rules:
Secret number = 1–100
User guesses
If guess is too low:
print "Too low"
If guess is too high:
print "Too high"
If correct:
print "Correct"
stop
Bonus:
count attempts
At the end:
You guessed it in 7 attempts.
160. Interview Questions#
Q1. What is a loop?#
A loop repeatedly executes a block of code while a condition or iteration rule allows it to continue.
Q2. What types of loops does Java provide?#
The main looping constructs are:
while
do-while
for
enhanced for
Q3. Difference between while and do-while?#
while checks the condition before executing the body.
do-while executes the body first and checks the condition afterward.
Therefore, a do-while executes at least once.
Q4. When should you use a for loop?#
A for loop is often appropriate when initialization, termination condition, and update naturally form a counter-controlled loop.
Q5. What is an enhanced for loop?#
It is a loop designed to iterate through elements of arrays and other iterable structures without manually managing an index.
Q6. What does break do?#
It exits the nearest enclosing loop or switch statement.
Q7. What does continue do?#
It skips the remainder of the current loop iteration and proceeds with the next iteration.
Q8. Difference between break and continue?#
break → stop loop
continue → skip current iteration
Q9. What is an infinite loop?#
A loop that does not terminate because its continuation condition never becomes false or it otherwise lacks a reachable exit.
Q10. Can an infinite loop be intentional?#
Yes. Event-processing systems, servers, and interactive programs can intentionally run continuously until an external or internal termination condition occurs.
Q11. What is a nested loop?#
A loop inside another loop.
Q12. How many times does a nested loop execute?#
If the outer loop runs n times and the inner loop runs m times for every outer iteration, the inner body executes:
n × m
times.
Q13. What is an off-by-one error?#
An error where a loop executes one iteration too many or one too few, often caused by incorrect use of < and <=.
Q14. What happens if you forget to update a loop counter?#
If the loop condition depends on that counter and it never changes toward termination, the loop may become infinite.
Q15. Can a for loop have no condition?#
Yes.
Example:
for (;;) {
}
This creates an infinite loop unless something exits it.
Q16. Can you use break in a while loop?#
Yes.
Q17. Can you use continue in a for loop?#
Yes.
Q18. Does break exit all nested loops?#
No. An ordinary break exits only the nearest enclosing loop.
A labeled break can target a labeled outer loop.
Q19. Does continue exit the loop?#
No. It skips the current iteration and continues with the next iteration.
Q20. What is a sentinel?#
A special input value used to indicate that processing should stop.
Example:
-1
can be chosen as a sentinel in a program that accepts non-negative values.
161. Interview Output Questions#
Question 1#
What is printed?
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
Answer:
1 2 3 4 5
Question 2#
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
Answer:
0 1 2 3 4
Question 3#
int i = 5;
while (i > 0) {
System.out.print(i + " ");
i--;
}
Answer:
5 4 3 2 1
Question 4#
int i = 10;
do {
System.out.println(i);
} while (i < 5);
Answer:
10
The body executes once before the condition is tested.
Question 5#
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.print(i + " ");
}
Answer:
1 2
Question 6#
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i + " ");
}
Answer:
1 2 4 5
Question 7#
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
System.out.println(sum);
Answer:
10
Question 8#
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print("*");
}
System.out.println();
}
Answer:
**
**
**
162. Advanced Thinking: Loop Correctness#
When solving a loop problem, don't just ask:
"What code should I write?"
Ask:
"What should be true after each iteration?"
Example:
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
At the end of iteration i, the intended meaning is:
sum = 1 + 2 + ... + i
This way of thinking becomes extremely useful when you later study algorithms, recursion, data structures, and problem solving.
163. Loop Problem-Solving Strategy#
When given a loop problem:
Step 1 — Understand what repeats#
Ask:
What operation is repeated?
Step 2 — Identify the changing value#
Examples:
counter
index
input
remaining digits
Step 3 — Define the stopping condition#
Ask:
When should repetition stop?
Step 4 — Decide how the state changes#
Examples:
i++
i--
number /= 10
Step 5 — Dry run#
Test with a small example.
This process is more valuable than memorizing loop syntax.
164. Loop Templates#
Template 1 — Count Up#
for (int i = start; i <= end; i++) {
// work
}
Template 2 — Count Down#
for (int i = start; i >= end; i--) {
// work
}
Template 3 — While#
while (condition) {
// work
// update state
}
Template 4 — Do-While#
do {
// work
} while (condition);
Template 5 — Enhanced For#
for (Type value : values) {
// process value
}
Template 6 — Search#
for (Type value : values) {
if (value == target) {
// found
break;
}
}
Template 7 — Accumulator#
int result = 0;
for (...) {
result += value;
}
165. Final Chapter Summary#
Loops allow Java programs to repeat work.
The main loop constructs are:
while
do-while
for
enhanced for
A while loop:
while (condition) {
...
}
checks its condition before every iteration.
A do-while loop:
do {
...
} while (condition);
runs its body at least once.
A for loop:
for (initialization; condition; update) {
...
}
is excellent for counter-controlled repetition.
An enhanced for loop:
for (Type value : values) {
...
}
is convenient when processing every element of an array or iterable structure.
Loop control:
break
→ exit the nearest loop
continue
→ skip the current iteration
return
→ exit the current method
Nested loops allow loops inside loops:
outer loop
↓
inner loop
They are useful for:
patterns
matrices
tables
pair comparisons
multi-dimensional data
The most important loop-design questions are:
Where do I start?
What condition means "continue"?
What work happens?
How does the state change?
When does the loop stop?
If you can answer these questions, loops become much easier.
166. What You Should Be Able to Do Now#
Before moving to Chapter 8, you should be comfortable writing:
1. for loops
2. while loops
3. do-while loops
4. enhanced for loops
5. nested loops
6. break
7. continue
8. counters
9. accumulators
10. searches
11. validation loops
12. number problems
13. String loops
14. array loops
15. basic patterns
You should also understand the difference between:
0 < 5
0 <= 5
and be able to explain why a loop stops.
167. Course Progress#
You have now completed:
01 Java Introduction
02 Setup & First Program
03 Variables & Data Types
04 Operators
05 Input & Output
06 Conditions
07 Loops ← YOU ARE HERE
The next chapter is:
Chapter 8 — Methods#
You will learn:
What is a method?
Why methods?
Method syntax
Parameters
Arguments
Return values
void
return
Method overloading
Variable scope
Local variables
Static methods
Calling methods
Pass-by-value
Recursion
Method design
Practical programs
Interview questions
Methods are extremely important because they teach you how to break a large program into small, reusable pieces.
Later, this idea becomes the foundation for classes and objects:
Methods
↓
Classes
↓
Objects
↓
Encapsulation
↓
Inheritance
↓
Polymorphism
↓
Abstraction
↓
Complete OOP