Loops & Iterations
🎯 Learning Path Progress
Current: Loops & Iterations | Next: Functions & Methods
🎓 Context Switcher
Loops (Iteration Statements)
🎯 Core Concept
Loops are programming constructs that allow repeated execution of a block of code based on a condition or over a sequence. They are fundamental to automation and repetitive task handling.
🔄 Types of Loops
Entry-Controlled Loops
Loops where the condition is checked before executing the loop body.
For Loop
Used when the number of iterations is known in advance.
# Python
for i in range(5):
print(f"Iteration {i}")
// Java
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + i);
}
While Loop
Used when the number of iterations is unknown and depends on a condition.
# Python
count = 0
while count < 5:
print(f"Count: {count}")
count += 1
// Java
int count = 0;
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
Exit-Controlled Loops
Loops where the condition is checked after executing the loop body.
Do-While Loop
Executes the loop body at least once before checking the condition.
// Java
int count = 0;
do {
System.out.println("Count: " + count);
count++;
} while (count < 5);
🎮 Loop Control Statements
Break Statement
Immediately exits the loop, skipping remaining iterations.
Continue Statement
Skips the current iteration and moves to the next one.
📚 Common Use Cases
- Processing collections - Iterating over arrays, lists, or datasets
- Input validation - Repeatedly prompting until valid input
- Mathematical calculations - Series, factorial, summation
- Game loops - Continuous game state updates
- File processing - Reading records until end of file
⚡ Performance Considerations
- Choose the right loop type based on the use case
- Avoid infinite loops by ensuring proper termination conditions
- Minimize work inside loops for better performance
- Consider built-in functions for common operations (map, filter, reduce)
This atomic content can be included in Python, Java, C++, and general programming tutorials.
📚 Practice Problems
Repetition is what computers do best. These problems will help you master for, while, and
do-while loops, as well as nested loops for patterns.
1. Print 1 to N
Rating: Newbie | Difficulty: Easy
Input a number N and print all numbers from 1 to N.
💡 Hint
Use a
for loop starting from 1 to N.
2. Even Numbers from 1 to N
Rating: Newbie | Difficulty: Easy
Input N and print all even numbers between 1 and N.
💡 Hint
Use if (i % 2 == 0) inside the loop, or increment the loop by 2: for (int i = 2; i <= N; i += 2).
3. Factorial of a Number
Rating: Beginner | Difficulty: Easy
Input a number and calculate its factorial (n!). E.g., 5! = 5 * 4 * 3 * 2 * 1 = 120.
💡 Hint
Initialize fact = 1 and multiply it by loop counter i in each
iteration.
4. Multiplication Table
Rating: Beginner | Difficulty: Easy Input a number and print its multiplication table up to 10.
💡 Hint
print(num + " x " + i + " = " + (num * i)).
5. Sum of Digits
Rating: Intermediate | Difficulty: Medium
Input a number and calculate the sum of its digits. E.g., 123 -> 1 + 2 + 3 = 6.
💡 Hint
Use num % 10 to get the last digit and num / 10 to remove it in a
while loop.
6. Reverse a Number
Rating: Intermediate | Difficulty: Medium
Input a number and print its reverse. E.g., 123 -> 321.
💡 Hint
reverse = (reverse * 10) + (num % 10).
7. Palindrome Number
Rating: Intermediate | Difficulty: Medium
Check if a number is a palindrome (reads the same forward and backward). E.g., 121 is a
palindrome.
💡 Hint
Reverse the number and compare it with the original value.
8. Fibonacci Series
Rating: Beginner | Difficulty: Easy
Print the first N terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13...
💡 Hint
Start with a = 0, b = 1. In each step, next = a + b, then update a = b and b = next.
9. Prime Number Checker
Rating: Intermediate | Difficulty: Medium Input a number and check if it is prime (divisible only by 1 and itself).
💡 Hint
Check divisibility from 2 up to √num. If any number divides it, it's not prime.
10. Armstrong Number
Rating: Advanced | Difficulty: Medium
Check if a number is an Armstrong number. (Sum of cubes of digits equals the number itself, e.g.,
153 = 1³ + 5³ + 3³).
💡 Hint
Extract each digit, cube it, and add to a sum.
11. GCD (Greatest Common Divisor)
Rating: Advanced | Difficulty: Medium Find the GCD of two numbers.
💡 Hint
Use the Euclidean algorithm or a loop from 1 to the smaller of the two numbers.
12. Power of a Number
Rating: Beginner | Difficulty: Easy
Input base and exponent. Calculate base^exponent without using built-in math functions.
💡 Hint
Run a loop exponent times and multiply base each time.
13. Star Pattern (Right Triangle)
Rating: Intermediate | Difficulty: Medium
Print the following pattern for N rows:
*
**
***
****
💡 Hint
Use nested loops. Outer loop for rows, inner loop for columns
(j <= i).
14. Number Pattern
Rating: Intermediate | Difficulty: Medium Print the following pattern:
1
12
123
1234
💡 Hint
Similar to star pattern, but print the inner loop counter j.
15. Perfect Number
Rating: Pro | Difficulty: Medium
Check if a number is a Perfect Number (sum of its proper divisors equals the number, e.g., 6 = 1 + 2 + 3).
💡 Hint
Loop from 1 to num/2 and add the divisors.