Repetition Statements
A repetition statement is a control structure that allows a set of instructions to be executed multiple times based on a condition or a specified number of iterations.
- Repetition statements are also known as loops.
- The formal term for repetition is iteration.
There are three main types of repetition statements:
- For loops: Used when the number of iterations is known.
- While loops: Used when the number of iterations is unknown and depends on a condition.
- Do...while loops: Similar to while loops, but the condition is checked after the loop body is executed, ensuring the loop runs at least once.
- Use for loops when you know exactly how many times you want to repeat a block of code.
- Use while loops when the repetition depends on a condition that may change during execution.
For loops
- A for loop is a repetition statement that iterates a specific number of times, controlled by an initialisation, a condition, and an increment or decrement operation.
- Its general structure is:
for (initialization; condition; update) {
// loop body
}
- Initialisation: This only happens once, at the very start of the loop.
- Condition: Before each loop run, Java checks if the condition is true.
- If i is still less than n, the loop continues.
- If the condition is false, the loop stops.
- Loop body: The statements inside { ... } run once for every loop iteration.
- Update: After the loop body finishes, the update runs.
Printing numbers from 1 to 5:
for (int i = 1; i <= 5; i++){ // i++ equals to i = i + 1
System.out.println(i)
}- The loop variable i is initialised to 1, and the loop continues as long as i <= 5.
- After each iteration, i is incremented by 1.
While Loops
A while loop is a repetition statement that executes a block of code as long as a specified condition is true.
while (condition) {
// statements to repeat
}
int i = 1;
while (i <= 5){
System.out.println(i);
i += 1;
}Do...While Loops
A do...while loop is a repetition statement that executes a block of code at least once and then repeats the loop as long as a specified condition is true.
do {
// loop body
} while (condition);
int i = 1;
do {
System.out.println(i);
i += 1;
} while (i < n);
- Do not confuse while and do-while loops!
- A while loop checks the condition before running, hence it may not run at all.
- A do-while loop checks the condition after running, hence it will always run at least once, even if the condition is false at the start.
- Always make sure the condition will eventually become false.
- Otherwise, the loop will never stop (infinite iteration).
int i = 0;
while (i < 5) {
System.out.println(i);
// forgot i++ 1→
// hence i never changes and results in infinite loop!
}
Tracing Repetition Statements
Tracing a loop involves following the execution of the loop step by step to understand how the variables change.
Consider the following code:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++){
System.out.println("Iteration no " + i);
}
}
}
- Iteration 1: i = 1, prints "Iteration no 1"
- Iteration 2: i = 2, prints "Iteration no 2"
- Iteration 3: i = 3, prints "Iteration no 3"
- Loop ends: i = 4, condition i <= 3 is false
- When tracing a loop, keep track of the loop variable and how it changes after each iteration.
- Consider putting it inside a table if you have many variables.
- Can you explain the difference between a for loop and a while loop?
- How would you use a loop to calculate the factorial of a number?