Repeat Pattern in Java

I teach beginners programming (from 12-15 years old), and one of the options we made (because it was natural in Python) was to teach the concept of "repeat action" before the concept of variables.

We took off in Python using

for loop in range(10): 

not to mention variable arrays and in C ++ with

 #define repeat(nb) for(int _loop = 0 ; _loop < (nb) ; _loop++) 

The idea was to hide the complexity of the classic cycle in order to insist on a “re-entry” part. We do not hide from students the fact that "repeat (10)" is not part of C ++, it is just a way to simplify learning.

At Pascal, we cannot do more than

 for loop := 1 to 10 do 

but this is normal because it is not so difficult to remember.

I was looking for something like this in Java, and I found that:

 import java.util.List; import java.util.AbstractList; class Range { public static List<Integer> size(final int end) { return new AbstractList<Integer>() { @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"); } } } 

 for (int loop : Range.size(10)) 

still easier to remember than

 for(int loop = 0 ; loop < 10 ; loop++) 

but there are two problems:

  • two variables are needed for transcoded loops: I don't think we can do much with this
  • We have warnings because the loop variable is not used

Do you see the best solution that we have?

Once again, we only want to provide some “tool” at the initial stage so that students can “repeat” actions before they know anything about the “variables”. We are not hiding from them, which are not in the language, and after several exercises (~ 80-100) we ask them to use the real syntax.

<h / "> We have about 20 exercises before introducing variables: some of them relate to printing texts, but basically we provide one library with objects that you can manipulate (therefore, variables are hidden in the state of the object). For example, you can think of a “turtle lottery.” Thus, the concept of “loop” can be manipulated and “seen” before entering explicit variables, and you can perform interesting exercises very quickly.

For example, in Python, where you want to visit each case of a 10x10 table once and only once, and then return to the starting point (lower left corner):

 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() 

This exercise is not so simple, but the syntax is really simple and allows the student to focus on the "algorithmic" part, and not on the "langage" part. After a few exercises, students begin to communicate, and we can introduce more syntactic and more complex concepts, such as variables.

+4
source share
5 answers

Do you really need to use Java for these exercises? If other languages ​​work for you, then why not use them? You can always switch to Java when students know the basics, such as variables.

I agree that variables can be quite confusing for beginners - especially that their value can constantly change, this is not something that people use from algebra, where the values ​​do not change after the “assigned” one.

If you want to use Java, you can use a while loop, which seems to be better suited. One dirty trick to avoiding using a variable is the following code: StackTraceElement is used instead.

He is typing

 Hello A Hello B Hello C Hello C Hello C Hello B Hello C Hello C Hello C Hello A Hello B Hello C Hello C Hello C Hello B Hello C Hello C Hello C Hello A Hello B Hello C Hello C Hello C Hello B Hello C Hello C Hello C 

Here is the complete source. main (Strinng [] args) method is code with loops, rest is supporting code.

 import java.util.HashMap; import java.util.Map; public class Repeater { public static void main(String[] args) { while(range(3)) { System.out.println("Hello A"); while (range(2)) { System.out.println("Hello B"); while (range(3)) { System.out.println("Hello C"); } } } } public static boolean range(int size) { return Range.range(size); } public static class Range { static Map<StackTraceElement, RangePosition> ranges = new HashMap<StackTraceElement, RangePosition>(); public static boolean range(int size) { final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3]; //System.out.println(stackTraceElement); RangePosition position = ranges.get(stackTraceElement); if (position == null) { position = new RangePosition(); position.size = size; ranges.put(stackTraceElement, position); } final boolean next = position.next(); if (!next) { ranges.remove(stackTraceElement); } return next; } } public static class RangePosition { int current,size; boolean next() { current++; return current <= size; } } } 

But I would prefer to use some kind of language that supports this naturally.

+5
source

I will always introduce variables. What are you going to do inside the loop without knowing the variables?

Also, it might be easier to use a while loop. The while loop head is much simpler and does not require variable definitions. It is very simple to understand:

 while (do_the_loop){ //this is repeated } 
+3
source

Java really is not suitable for this kind of task, because it does not allow the transfer of functions.

The only way I can think of this without variables is through an interface:

 private static void repeat(int times, DoStuff what) { for (int i = 0; i < times; i++) { what.doIt(); } } private interface DoStuff { public void doIt(); } 

And then use it like this:

 repeat(5, new DoStuff() { public void doIt() { System.out.println("xx"); // whatever needs to be done }}); 

Which would be without variables, but confusing at the beginning.

+1
source

Actually, this is not what you are looking for, but here is my opinion and how I will teach it. (Beware, I'm not a teacher at all: D)

Are there not two different courses for this: Algorithms and programming? In programming, you really have to start by learning variables. This is not difficult. And I don’t think that 20 exercises about algorithms will be interesting for them. I think they are more attracted to the power to figure out something than to write for loops inside each other. Writing a simple program that calculates some sums and multiplications, etc., allows you to introduce the idea of ​​primitive variables. Then I would introduce a for loop. Demonstrate by typing 10 times the text in combination with the variable used in the for loop:

 for (int i = 0; i < 10; ++i) // Tell them that the 'i' is a variable as you explained before. { System.out.println("This is line " + i); } 

Then writing an application that calculates the factorial of a hard number will be interesting, I think. Thus, most of them, I hope, will get the idea of ​​working with variables. Then try to explain the area.

 int number = 5; int factorial = 1; for (int i = 1; i <= number; ++i) { factorial = factorial * i; } System.out.println(number + "! = " + factorial); 
+1
source

if you have a limit set, you need to use the while structure

while (myval <mylimit) {

// do something

myval ++; }

but when the limit is unknown, you should use for or for each

for (int i = 0; i <list.size (); i ++) {

} from objects

for each (String myString: myStringArray) {

}

Saludos

-1
source

Source: https://habr.com/ru/post/1390979/


All Articles