For Loop in Java
1. Introduction
- Purpose of loops in programming.
- Overview of the 'for' loop.
2. Syntax of a For Loop
General structure:
for (initialization; condition; update) {
// Code block to execute
3. Types of For Loops
- Simple For Loop: Iterating a fixed number of times.
- Nested For Loops: Using a loop inside another loop.
- Enhanced For Loop (For-Each Loop): Iterating through arrays and collections.
4. Examples
- Printing numbers from 1 to 10.
- Summing up elements of an array.
- Generating a multiplication table.
- Displaying patterns (e.g., triangles, squares).
5. Common Mistakes
- Infinite loops.
- Off-by-one errors.
- Modifying a collection during iteration.
6. Use Cases and Applications
- Iterating over arrays or lists.
- Repeating tasks a fixed number of times.
7. Practice Exercises
- Write a program to find the factorial of a number using a 'for' loop.
- Create a pattern using nested 'for' loops.
- Calculate the sum of odd numbers between 1 and 50.