title: Number of Elements in Array that are lower or equal to
body: <pre><code>public class ArrayExercise
  {
      public static void main(String [] args)
      {
          int arr[] = { 3, 4, 14, 32, 45, 61, 32, 18, 9, 38, 99, 42 };
          int sentinel = 42;
          int count = 0;
          ;
  
          for (int i = 0; i &lt; arr.length; i++) {
  
              if (arr[i] != sentinel) {
                  count++;
              }
              else {
                  break;
              }
          }
  
          System.out.println(count);
      }
  }
  </code></pre>
  <p>I want to count all the elements in the array that are less than 23 using 42
  as a comparison value (Sentinel Value). I am stuck on how to implement the second part of the comparison. Currently my array only counts the number of elements that are not 42.</p>
  <p>The expected output is 5 since 5 of the elements coming before 42 are less than 23 (3, 4, 14, 18 and 9 are). Observed output from my code is 11.</p>
  <p>By sentinel value I mean that if the array is not completely filled out, counting should still stop when the sentinel value, 42, is encountered. Example:</p>
  <pre><code>int arr[] = { 3, 14, 32, 45, 18, 38, 42, 0, 0, 0, 0, 0 };
  </code></pre>
  <p>Here the expected output is 3 because 3 of the elements before 42 are less than 23 (3, 14 and 18).</p>