Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(java) using code provided below: Which one is better, the for loop or the while loop? provide a detailed explanation. Note* i originally thought the
(java)
using code provided below:
Which one is better, the for loop or the while loop? provide a detailed explanation.
Note*
i originally thought the for loop would be better as it would be more direct. The while loop would have no condition and end once array target is found. However, the times stamps in the program below indicate the while loop is faster.
//code to copy
import java.util.Arrays; import java.util.Scanner; class NotFoundException extends Exception //creating NotFoundException { public NotFoundException (String message) { super (message); } } public class Main { public static int searchForLoop (int a[], int x) throws NullPointerException, NotFoundException { long startTime = System.nanoTime (); //start time of the program int index = 0; try { for (;; index++) { if (a[index] == x) { System.out.println(x + " found at index " + (index +1 ) + " in for loop array"); long endTime = System.nanoTime(); //endTime if found element long totalTime = endTime - startTime; //total run time System.out.println("total run time in nanosecond:" + totalTime); return index; } } } catch (IndexOutOfBoundsException e) //if element not found in array { throw new NotFoundException ("X not found in given array by searchForLoop"); } } public static int searchWhileLoop (int a[], int x) throws NullPointerException, NotFoundException { long startTime = System.nanoTime (); //start time of the program int i = 0; try { while (true) { if (a[i++] == x) { System.out.println(x + " found at index " + (i) + " in while loop array"); long endTime = System.nanoTime(); //endTime if found element long totalTime = endTime - startTime; //total run time System.out.println("total run time in nanosecond:" + totalTime); return i; } } } catch (IndexOutOfBoundsException e) { throw new NotFoundException ("X not found in given array by searchwhileLoop"); } } public static void main (String[]args) { long startTime = System.nanoTime (); //start time of the program int a[] = { 30, 10, 3, 40, 45, 89, 90, 23, 60 }; ///array a[] Arrays.sort (a); //sorting the array try { Scanner sc = new Scanner (System.in); //taking the input System.out.println ("Enter the element to search"); int x = sc.nextInt (); // int found = searchForLoop (a, x); //calling searchForLoop function searchForLoop (a, x); searchWhileLoop (a, x); } catch (NotFoundException e) { long endTime = System.nanoTime (); //end time if not found long totalTime = endTime - startTime; //total run time System.out.println ("total run time in nanosecond:" + totalTime); e.printStackTrace (); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started