title: Running several iterations in a while loop
body: <p>I am trying to solve a problem of finding the smallest and second smallest element in an array. </p>
  
  <p>I am thinking of putting two pointers on the 0th index of the array. Both the pointers move from left to right traversing the entire array. First pointer ptr1 determines the min element while the second pointer intends to determine the second min element. The first pointer works ok but the second pointer doesn't traverse. While loop exits only after 1 iteration of the second pointer. </p>
  
  <blockquote>
    <p>Is it possible to have n pointers in a while loop &amp; make them traverse
    from left to right turn by turn?</p>
  </blockquote>
  
  <p>Or I am doing something wrong.</p>
  
  <p>Below is the code</p>
  
  <pre><code>int arr[] = {12,13,1,10,34,1}; 
          int ptr1 = 0;
          int ptr2 =0;
          int min = Integer.MAX_VALUE;
          int minSec = Integer.MAX_VALUE;
          int arrLen=arr.length-1;
  
          while(ptr1&lt;arrLen &amp;&amp; ptr2&lt;arrLen){
  
              if(arr[ptr1]&lt;min){  // this if works great finds the min element
                  min=arr[ptr1];
                  ptr1++;
              }else{
                  ptr1++;
              }
  
              //flow enters once &amp; exits the while loop
              if(ptr1==arrLen &amp;&amp; arr[ptr2]&lt;minSec &amp;&amp; arr[ptr2]&gt;min){
                  minSec=arr[ptr2];
                  ptr2++;
              }else if(ptr1==arrLen){
                  ptr2++;
              }
          }
  
        System.out.println("min: " + min + " second min: "+ minSec)
  </code></pre>
  
  <p>output: <code>min: 1 second min: 12</code></p>
  
  <p>the correct output should be <code>min: 1 second min: 10</code></p>
  
  <p>I am able to solve the problem with another approach, code below. I just need to know about the while loop approach.</p>
  
  <pre><code>for (int i = 0; i &lt;= arrLen ; i ++)
          {
              /* If current element is smaller than first
                then update both first and second */
              if (arr[i] &lt; min)
              {
                  minSec = min;
                  min = arr[i];
              }
  
              /* If arr[i] is in between first and second
                 then update second  */
              else if (arr[i] &lt; minSec &amp;&amp; arr[i] != min)
                  minSec = arr[i];
          }
  </code></pre>