# Own programming language: How nested for-loop?

**URL:** <https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396>\
**Category:** Coding Questions\
**Created:** [December 20, 2020, 11:18am UTC](https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396 "2020-12-20T11:18:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 20, 2020, 11:18am UTC](https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396/1 "2020-12-20T11:18:29Z")

</div>

Hello all,

I am doing my own programming language at the moment.

An interpreter language.

When a for-loop is, not so difficult. I have the start line and he jumps x-times back to the start line ( “{” ) from the end line ("}") of the for loop. These are 3 global variables.

BUT how can I do a nested for loop? I don’t whether there are 2 or 7 nested for loops.

So, do I have to use a stack? To replace my 3 global variables?

A stack like:

`ArrayList< ForLoopData> = new ArrayList();`

and

```auto
class ForLoopData {

       int max_i_value; 
       int current_i_value; 
       String variableName;

}//class

```

Thank you!

Chrisir

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [December 21, 2020, 3:41pm UTC](https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396/2 "2020-12-21T15:41:55Z")

</div>

> [@Chrisir](#):
>
> These are 3 global variables

that’s your issue. don’t use global variables, that should be done through indeed a stack or through the use of recursive calls (which will use the stack)

it also depends on how your code parser works

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 21, 2020, 7:11pm UTC](https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396/3 "2020-12-21T19:11:17Z")

</div>

Okay, thank you…

😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 26, 2020, 11:20am UTC](https://discourse.processing.org/t/own-programming-language-how-nested-for-loop/26396/4 "2020-12-26T11:20:57Z")

</div>

I solved it using a stack to  
hold the for-loop data

Thank you!

Chrisir

**Here is the code**

(the usage of this is elsewhere)

```auto
// see https://de.wikipedia.org/wiki/Stapelspeicher#Stapelorientierte_Sprachen

class ForLoopData {

  int max_i_value; 
  int current_i_value; 
  int nodeStartNumber; 
  String variableName;

  //constr 
  ForLoopData ( int max_i_value_, 
    int current_i_value_, 
    int nodeStartNumber_, 
    String variableName_ ) {

    max_i_value = max_i_value_; 
    current_i_value = current_i_value_;
    nodeStartNumber = nodeStartNumber_; 
    variableName = variableName_;
  }//constr 
  //
}//class

// =================================================================

class Item {
  ForLoopData value; // Das zu verwaltende Objekt
  Item next; // Referenz auf den nächsten Knoten

  Item(ForLoopData _value, Item _next) {
    value = _value;
    next = _next;
  }
}//class

// =================================================================

class Stack {

  Item _peek = null;
  int counter = 0; 

  // Prüft, ob der Stapel leer ist
  boolean empty() {
    return 
      _peek == null;
  }

  // Speichert ein neues Objekt
  void push(ForLoopData _value) {
    _peek = new Item(_value, _peek);
    counter++;
  }

  // Gibt das oberste Objekt wieder und entfernt es aus dem Stapel
  ForLoopData pop() {
    ForLoopData temp = _peek.value;

    if (!empty())
      _peek = _peek.next;

    counter--;
    return temp;
  }

  // Gibt das oberste Objekt wieder
  ForLoopData peek() {
    return !empty() ? _peek.value : null;
  }//method
  //
}//class
//

```
