Lesson 4: Definite Iterative Constructs

What is Definite Iteration?

A definite iteration is a type of iteration where the number of iterations is known before the loop starts — think of a for loop, where you know exactly how many times the loop will run.

When to Use Definite Iteration

  • When the number of iterations is fixed.
  • When you need to perform a task a specific number of times.

Worked Examples

Calculate the sum of the first 10 positive integers:

int sum = 0;
for (int i = 1; i <= 10; i++) {
  sum += i;
}
System.out.println("Sum: " + sum);

Print the first 5 terms of the Fibonacci sequence:

int a = 0; int b = 1;
for (int i = 1; i <= 5; i++) {
  System.out.print(a + " ");
  int temp = a;
  a = b;
  b = temp + b;
}

Leave a comment

Your email address will not be published. Required fields are marked *

sponsors Ads