title: The loop runs twice in a single run
body: <p>This problem is from <a href="https://www.hackerrank.com/" rel="nofollow noreferrer">https://www.hackerrank.com/</a> and link to it is <a href="https://www.hackerrank.com/challenges/java-list/problem" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/java-list/problem</a> .
  In the below code while loop is running twice as according to question we need to enter Q, Q times an operation to perform in the Array Declared. For this, i am running twice the loop so that I can get the desired result. </p>
  
  <pre><code>import java.util.*;
  
  public class javaList {
  
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int N = sc.nextInt();
          int i, x;
          ArrayList L = new ArrayList(N);
          for (i = 0; i &lt; N; i++) {
              L.add(sc.nextInt());
          }
          int Q = sc.nextInt();
          i = 0;
          // for normal running i have multiplied Q by 2 so that i can get the results
          while (i &lt; Q * 2) {
              System.out.println("Loop: " + i);
              String s = sc.nextLine();
              int sz = L.size();
              // code for checking insert
              if (s.equals("Insert")) {
                  x = sc.nextInt();
                  int y = sc.nextInt();
                  //if the position i am looking exists then just replace
                  // i need to insert at index x of array L but array.size() gives one more than the last index
                  if ((sz - 1) &gt;= x) {
                      L.add(x, y);
                  }
                  //if the position i am looking does not exist then create
                  else {
                      for (int j = sz; j &lt;= x; j++) {
                          //add number to desired place
                          if (j == x)
                              L.add(y);
                              //in between the two endings of array and insertion adding default value 0
                          else
                              L.add(0);
                      }
                  }
                  //checking code for Delete
              } else if (s.equals("Delete")) {
                  x = sc.nextInt();
                  //if the desired location exists then only replace
                  if ((sz - 1) &gt;= x) {
                      L.remove(x);
                  }
              }
              i++;
          }
          for (i = 0; i &lt; L.size(); i++) {
              System.out.print(L.get(i) + " ");
          }
      }
  }
  </code></pre>
  
  <p>I want to Know why the loop is running twice in a single run.</p>