Loops & Iterations¶
Prerequisites: Basic variables, Conditional statements
🎯 Learning Path Progress¶
🎓 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.
While Loop¶
Used when the number of iterations is unknown and depends on a condition.
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.
🎮 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 afor 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
Useif
(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 witha = 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:
💡 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 tonum/2 and add the divisors.