Java Revision Sheet – Unit 5: Iterative Construct
(for loop only)
■ What is Iteration?
Iteration means repeating a set of instructions until a condition is met. In Java, we use loops for
iteration.
■ Types of Loops in Java
1. for loop ■ (in syllabus) 2. while loop ■ (not in syllabus) 3. do-while loop ■ (not in syllabus)
■ The for Loop – Syntax
for(initialization; condition; update)
{
// statements to be repeated
}
Example:
for(int i = 1; i <= 5; i++)
{
[Link](i);
}
Output: 1 2 3 4 5
■■ How a for loop works
1■■ Initialization – runs once 2■■ Condition check – if true, execute body 3■■ Execute body –
run statements 4■■ Update – increase or decrease variable 5■■ Repeat until condition becomes
false
■ Excluded in syllabus: Nested loops (loop inside another loop)
■ Important Examples
Print numbers from 1 to 10
for(int i = 1; i <= 10; i++)
{
[Link](i);
}
Print even numbers from 2 to 20
for(int i = 2; i <= 20; i = i + 2)
{
[Link](i);
}
Print numbers in reverse (10 to 1)
for(int i = 10; i >= 1; i--)
{
[Link](i);
}
Find sum of first 10 natural numbers
int sum = 0;
for(int i = 1; i <= 10; i++)
{
sum = sum + i;
}
[Link]('Sum = ' + sum);
■ Keywords to Remember
Initialization: Start value (e.g., int i = 1) Condition: When to stop looping (e.g., i <= 10) Update:
Change after each loop (e.g., i++) Iteration: One complete cycle of loop execution