title: Java 'for' loop with range
body: <p>I am working on a basic program (console).</p>
  
  <p>The program should allow the user to enter in various marks, until the mark entered exceeds 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.</p>
  
  <p>This is an example of the output. </p>
  
  <p><strong>0 - 29 xxx</strong></p>
  
  <p><strong>30 - 39   xxxxx</strong></p>
  
  <p><strong>40 - 69   xxxxxxx</strong></p>
  
  <p><strong>70 - 100  xxxx</strong></p>
  
  <p>20 students in total.</p>
  
  <p>As the user enters each mark, there ought to be a counter that increases in value and print the total number of marks entered.</p>
  
  <p>I want to make sure that the program is as efficient as possible but also understandable</p>
  
  <p><strong>Code</strong></p>
  
  <p>Working with @Dici on collabedit I have an amazing answer to my question:</p>
  
  <pre><code>public static void main(String[] args) {
  
      Scanner sc = new Scanner(System.in);
  
      int[] ranges  =  { 0,29,39,69,100 };
      int[] inRange = new int[ranges.length - 1];
  
      int mark;
      do {
          System.out.println("Enter Mark:");
          mark = sc.nextInt();           
          for (int j=1 ; j&lt;ranges.length ; j++)
             if (ranges[j-1] &lt;= mark &amp;&amp; mark &lt;= ranges[j]) {
                 inRange[j-1]++;
                 break;
             }
      } while (mark &lt;= 100);
  
      System.out.println(Arrays.toString(inRange));
  
      String s = "The number of students that have scored between %d and %d is: ";
      int k = 0;
      for (int i=0 ; i&lt;ranges.length - 1 ; i++) {
        System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));   
          for (int r = 0; r&lt;inRange[i] ; r++)
              System.out.print("*");
          System.out.println();
          k = 1;
      }
  
      sc.close();
  </code></pre>
  
  <p>Thank you again for your amazing help!</p>