What is a for Loop?
A for loop is used when:
- The number of iterations is fixed
- We know the starting point and ending point
- We want to repeat code a specific number of times
Syntax
for (initialization; condition; increment/decrement) {
// code to be executed
}
How it works:
- Initialization happens once
- Condition is checked before each iteration
- If condition is true → loop body executes
- After execution, increment/decrement happens
- Control goes back to condition check
- When condition becomes false → loop stops
Order of Execution in a for Loop
1. Initialization
The loop control variable is initialized.
2. Condition Check
The condition is checked before executing the loop body.
3. Loop Body Execution
If the condition is true, the statements inside the loop execute.
4. Increment / Decrement
The loop variable is updated.
5. Repeat
Control goes back to the condition check.
6. Termination
When the condition becomes false, the loop ends.
Important Point to Note
The for loop is an entry-controlled loop, so if the condition is false initially, the loop will not execute even once.
Key Points
-
forloop checks condition before execution - Best suited when iteration count is known beforehand
- Initialization, condition, and update are written in one line
- Helps write clean and readable code
- Commonly used for ranges, tables, and counting problems
Task 1: Print Numbers from 1 to 20
for (int i = 1; i <= 20; i++) {
System.out.println(i);
}
Explanation:
The loop starts with i = 1. The condition checks whether i is less than or equal to 20. If true, the value of i is printed. After printing, i is incremented by 1. This continues until i becomes greater than 20.
Task 2: Print All Odd Numbers from 1 to 50
for (int i = 1; i <= 50; i++) {
if (i % 2 != 0) {
System.out.println(i);
}
}
Explanation:
The loop runs from 1 to 50. Inside the loop, the condition checks whether the number is odd. If it is odd, the number is printed. The loop continues until the value reaches 50.
Task 3: Find the Sum of Even Numbers from 1 to 100
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum = sum + i;
}
}
System.out.println("Sum of even numbers: " + sum);
Explanation:
The loop runs from 1 to 100. Only even numbers are added to the sum variable. After the loop finishes, the total sum of even numbers is printed.
Task 4: Print the 7th Multiplication Table
for (int i = 1; i <= 10; i++) {
System.out.println("7 x " + i + " = " + (7 * i));
}
Explanation:
The loop runs from 1 to 10. In each iteration, the value of i is multiplied by 7 and printed in table format. The loop stops after completing 10 iterations.
Task 5: Count How Many Numbers Are There from 1 to 100
int count = 0;
for (int i = 1; i <= 100; i++) {
count++;
}
System.out.println("Total count: " + count);
Explanation:
The loop runs from 1 to 100. Each iteration increases the count variable by 1. After the loop ends, the value of count represents the total number of values between 1 and 100.
Common Mistakes to Avoid
- Using wrong condition (
<instead of<=) - Writing incorrect increment or decrement
- Forgetting semicolons inside the
forstatement - Modifying loop variable incorrectly inside the loop
When to Use a for Loop?
Use the for loop when:
- You know how many times the loop should run
- The loop depends on a fixed range
- Printing tables, ranges, or counting values
Top comments (0)