πŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
β†’ Click Here ←

Iterative Approach  


Introduction
  • The iterative approach is an approach which solves a problem by using loops such as for, while or do-while instead of recursion.
  • It repeatedly executes a block of code until a condition becomes false.
  • It improves performance by avoiding the overhead of recursive function calls.
  • In simple words, iterative approach uses loops to repeat tasks efficiently.
  • Advantages:
    • Faster and memory-efficient compared to recursion.
    • Avoids stack overflow issues.
    • Easy to debug as control flow is straightforward.
    • Better performance for large inputs.
  • Disadvantages:
    • Code may become lengthy for problems that have recursive nature (like Tree or Graph traversal).
    • Sometimes harder to visualize compared to recursion.
    • Not suitable for divide and conquer type problems.
Examples of Iterative Approach
  1. Using Loops for Linear Problems:
    • Iterating through every element in a list or array.
    • Example: Finding the sum of all elements in an array
      public class SumOfArray
      {
          public static void main(String[] args)
          {
              int[] arr = {4, 7, 1, 3, 6};
              int sum = 0;
      
              for(int i = 0; i < arr.length; i++)
              {
                  sum += arr[i]; // adding each element
              }
      
              System.out.println("Total Sum: " + sum);
          }
      }
  2. Iterating with Conditions:
    • Applying loops with decisions inside.
    • Example: Counting even numbers in an array
      public class CountEven
      {
          public static void main(String[] args)
          {
              int[] arr = {2, 5, 8, 11, 14};
              int count = 0;
      
              for(int i = 0; i < arr.length; i++)
              {
                  if(arr[i] % 2 == 0)
                  {
                      count++;
                  }
              }
      
              System.out.println("Total Even Numbers: " + count);
          }
      }
  3. Using Nested Loops for Comparisons:
    • Checking all pairs or comparisons between elements.
    • Example: Bubble Sort (Iterative Sorting)
      public class BubbleSort
      {
          public static void main(String[] args)
          {
              int[] arr = {5, 3, 8, 1, 2};
      
              for(int i = 0; i < arr.length - 1; i++)
              {
                  for(int j = 0; j < arr.length - i - 1; j++)
                  {
                      if(arr[j] > arr[j+1])
                      {
                          // swap
                          int temp = arr[j];
                          arr[j] = arr[j+1];
                          arr[j+1] = temp;
                      }
                  }
              }
      
              // print sorted array
              for(int num : arr)
              {
                  System.out.print(num + " ");
              }
          }
      }

Iterative Approach Task:

Try below programs using Iterative Approach in Java:

  1. WAP to Move Zeros to End of an Array
  2. WAP to Search an Element in an Array
  3. WAP to Merge Two Arrays into One
  4. WAP to Remove Duplicates from an Array using Loops
  5. WAP to Reverse a String using Iterative Method