Besides structures like "for", "while", "goto" or recursion, is there any other way to repeat something in the programming?

Asked

Viewed 171 times

10

In structured programming, we have structures as links for, while and similar structures, in addition to goto, that allow us to realize repetitions. In functional programming, recursion is used to achieve the same goal.

Thus, in addition to repeating structures such as for, while, goto and recursion, in programming there is some other way to achieve loops? If yes, which?

2 answers

7


Iteration

Actually the first three he used is the iteration. If you consider everything as one thing, other than recursion, as far as I know, there are only these two.

There are other ways to iterate that are not these commands, but the question indicates knowing this. Some paradigm may even use another way to call it, but it will still be an iteration. Or recursion. Depending on abstraction there are other possible commands in some languages, but it is only syntax, it is not a new concept.

And iterations end up becoming one branch. The other commands are abstractions (project patterns). Even the goto ends up being an abstraction because the branch simple usually has less restrictions.

And it is good to make clear that the goto can do repetitions, but there’s no guarantee it was used like this.

Recursion

There is a recursion in different ways than what you know, for example templates, or even said that it can occur with Generics (I’ve never seen it, but I believe it does), so it’s not just a function. Macros can also have something like this. But it is common that these recursions do not happen in the execution, it is solved at compile time.

Remembering that it is common in some languages that function recursion is transformed into iteration when compiled. Then in your code you will have recursion, but internally you will only have one iteration.

So what you see in your auto-repeat recursion code can disappear from the executable, either by unrolling for manual repetition or iteration transformation.

Handbook

These forms of repetition are mechanisms for automating a repetition. It is obviously possible to repeat manually, but I understand that this would be unwanted to what is in the question.

For example the application proliferate around is a manual form, not a repetition mechanism written in code.

Abstractions from previous concepts

Of course it can be creative if it can involve other abstractions (through library and/or rather complex code, one example is corotine, another is GUI, all will have one Event loop masked iterative), but will still use one of the above concepts.

If it involves resources external to the application it is also possible to repeat something, but in the background there will be an iteration or recursion, even if it is not in your code or application. We could call it signaling.

Will you consider these as different forms of repetition? Whatever, but they are just abstractions on top of the two already established concepts.

If it can involve something external to the language there are infinite ways to do and it would not fit to answer here, the list will always be incomplete.

Since the question does not involve a specific technology, I understand that it refers to the mathematical concept and what happens in its code and I could only identify these two.

Condition

Also I will say that without some trick that hides a condition, such as an external signaling that interferes in the flow for example, it is not possible to have automatic finite repetition without some form of condition.

5

Some mechanisms different from those cited:

  • Corollaries.

  • It is also possible to do with creating subprocesses, where a process starts a copy of itself. Some computer viruses/worms do this. Once or twice I’ve seen it used in a legitimate program, but it’s rare.

  • Function that schedules itself to run again. Example in javascript:

    function teste() {
        alert("Hello");
        setTimeout(teste, 3000);
    }
    teste();

  • Function that runs periodically. Example in javascript:

    setInterval(function(){ alert("Hello"); }, 3000);

  • Many viruses/worms send themselves to other computers over the internet, then starting a new run of themselves on one another host network. This is also a form of repetition.

  • Use of buffer overflow faults in C allows some tricks to be made possible by writing directly to the memory representing the execution stack or code being executed, and with this it is possible to make:

    • A function x calls a function y. But when y returns, it will return to a point of x different from where y was called.
    • A function x call a function y. But when y returns, it will return to the middle of a function z completely different from x.
    • A function that writes a copy of itself somewhere in memory and calls this newly created function.
    • A function that changes its own code as it is running.


    Anyway, these crazy things I’ve only seen in four different places: (a) viruses (which do this to mask their purpose and hinder reverse engineering); (b) security breach exploits; (c) piracy protection codes and (d) IOCCC.

It is not very difficult to reduce the problem of defining what is or is not a new form of repetition to the parade problem. This implies that the number of ways to define repeating structures is essentially infinite. There will always be some newer and more sophisticated method of doing this, no matter how many previous methods have already been invented. It turns out that most of these forms are either niche things and very specific situations or they’re too crazy to make sense of being done by mere mortals. Anyway, the answer to the question "in programming there is some other way to perform loops?" is affirmative.

  • Which is what I said in "Of course you can be creative if you can involve other abstractions or resources external to the application, but in the background there will be an iteration or recursion, even if it’s not in your code or application" and "Moreover I will say that without some trick that hides a condition, such as an external signaling that interferes in the flow for example, it is not possible to have automatic finite repetition without some form condition", it is not a mechanism of the code. I didn’t list since I understood that he was talking about code mechanisms. A scheduler does repetition.

  • The setTimeout() asks for the Runtime along with the operating system do the repeat, not the code. And in practice it’s still a recursion or iteration even if you don’t see it (so I don’t even know which one was used) "Depending on abstraction there are other possible commands in some languages, but it’s just syntax, not a new concept", which is the case of a coroutine. Since it does not have a specified language, to list all abstractions would have to see all possible abstractions already created in a language or library, nor would it consider those that the programmer could create.

  • @Maniero It would be recursion if the function calls itself. In this case, it doesn’t call itself, it just asks for something else to call it. In addition, in a recursion, the result of the internal call is made available to the external one, which does not occur in a scheduling. Recursion also typically displays the call stacking of the same function on the runstack, which does not occur in scheduling. Therefore, this is not recursion, nor for, nor while and neither goto. It is true that there will be a while somewhere in the operating system or Runtime, but it is not being used directly.

  • @Maniero And it is not very difficult to reduce the problem of defining what is or is not a new form of repetition to the halting problem. This implies that the number of ways to define repeating structures is essentially infinite. There will always be some newer and more sophisticated method of doing this, no matter how many previous methods have been invented.

  • You do not need to call yourself to be recursion, there is triangulated recursion. But that doesn’t matter, the fact is that if you don’t have a recursion there will be an iteration. Precisely in order not to enter into the infinite of the answer it is better to reduce to the abstract concepts, because if the question wanted a list of all possible abstract mechanisms to create, it would be too broad, perhaps the widest ever created on the site.

  • 1

    @Maniero The question is only asking that "in programming is there any other way to make loops?" - And certainly there is. It turns out that most of these forms are either niche things and very specific situations or they’re too crazy to make sense of being done by mere mortals. But that doesn’t make the answer turn negative.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.