title: "repeat" structure in Java
body: <p>I'm teaching programming to beginners (starting at 12-15 years old) and one of the choices we made (because it was natural in Python) was to teach the notion of "repeating an action" before the notion of variables.</p>
  
  <p>We warted in Python with</p>
  
  <pre><code>for loop in range(10):
  </code></pre>
  
  <p>without speaking about variables of arrays and in C++ with</p>
  
  <pre><code>#define repeat(nb) for(int _loop = 0 ; _loop &lt; (nb) ; _loop++)
  </code></pre>
  
  <p>The idea was to hide the complexity of a classical loop in order to insist on the "repeat" part. We are not hiding from the students the fact that "repeat(10)" is not a part of C++, 
  it's just a way to simplify the learning.</p>
  
  <p>In Pascal we can't do much more than</p>
  
  <pre><code>for loop := 1 to 10 do
  </code></pre>
  
  <p>but that's ok because its's not that difficult to remember.</p>
  
  <p>I was looking for something similar in Java and I found that : </p>
  
  <pre><code>import java.util.List;
  import java.util.AbstractList;
  
  class Range {
      public static List&lt;Integer&gt; size(final int end) {
          return new AbstractList&lt;Integer&gt;() {
              @Override
              public Integer get(int index) {
                  return 0 + index;
              }
              @Override
              public int size() {
                  return end;
              }
          };
      };
  }
  
  public class Main {
       public static void main(String[] argv) {
      for (int loop : Range.size(10)) {
          System.out.println("xx");
      }
      }
  }
  </code></pre>
  
  <p>The</p>
  
  <pre><code>for (int loop : Range.size(10))
  </code></pre>
  
  <p>is still easier to remember than</p>
  
  <pre><code>for(int loop = 0 ; loop &lt; 10 ; loop++)
  </code></pre>
  
  <p>but there is two problems :</p>
  
  <ul>
  <li>two variables are needed for imbricated for loops : I dont think we can do much about that</li>
  <li>we are having warnings because the variable <code>loop</code> is not used</li>
  </ul>
  
  <p>Do you see a better solution that what we have ?</p>
  
  <p>Once again, we only want to provide some "tool" at the beginning phase in order for the students to "repeat" actions, before knowing anything about "variables". We are not hiding from them that's is not in the langage and after a few exercises (~80-100) we are asking them to use the real syntax.</p>
  
  <p><hr/>
  We have approximately 20 exercices before introducing variables : some about printing texts but mostly we are providing one library with objects you can manipulate (hence the variables are hidden in the object state). You can think of the "logo-turtle" for example. This way the notion of "loop" can be manipulated and "seen" before introducing explicit variables and you can have interresting exercises really fast.</p>
  
  <p>One example, in Python, where you want to visit every case of a 10x10 table once and only once and then be back at your starting point (lower-left corner) : </p>
  
  <pre><code>from robot import *
  
  top()
  for loop in range(4):
     for loop in range(8):
        top()
     right()
     for loop in range(8):
        bottom()
     right()
  for loop in range(8):
     top()
  right()
  for loop in range(9):
     bottom()
  for loop in range(9):
     left()
  </code></pre>
  
  <p>This exercise is not that easy but the syntax is really simple and allow the student to concentrate on the "algorithmic" part and not the "langage" part.
  After a few exercises the students are getting interrested and we can introduce more syntax and more difficult concepts like the variables.</p>