Can I use procedures to make an infinite loop?

As shown above.

There is no straigtforward "infinite loop" in App Inventor. However, the "while test" block tests the socket. If the socket value is true, it executes the "do" part; if it is false, then the whole block terminates.

image

http://ai2.appinventor.mit.edu.ezproxy.canberra.edu.au/reference/blocks/control.html#while

image

1 Like

Hi @schoolboi, why don't You use a "while do" loop if in the test You write an always true condition You have an infinite loop.
Best Regards
Marco

P.S. I don't know what kind of app You are developing, but You have to think to an exit strategy from the infinite loop, otherwise how can You close the app?

Probably better / more controllable to use a clock timer.

3 Likes

A clock timer stops the app from going into a coma.

Here is a sample Gallery project as an example:
http://ai2.appinventor.mit.edu.ezproxy.canberra.edu.au/?galleryId=5214268554739712

Demo

1 Like

It occurred to me that you might be coming from an Arduino or IOT coding background, where you retain control forever and loop over time.

The AI2 environment is Event based, where you put things into motion on their own (

  • start file operations
  • ask for web data
  • start a Sprite or Ball moving
  • ask for input
  • etc.)

and you have to take a leap of faith and let go of control,
leaving the rest of your code in the appropriate event block that will receive control after
your requested operation completes, for example

  • file operation (read/write) completes
  • Web data arrives
  • Sprite or Ball collides with another or hits the edge of the Canvas
  • user Clicks a Submit button
  • etc.

It is unfortunate that the AI2 reference helps for such components don't mention this architectural style more often.

Yes, I come from the IOT coding background, and learnt Python, HTML, and Scratch. But I am whole new to MIT App Inventor so I do not know how to use clocks and etc.

With Clock, you can ask clock to raise/trigger an event, within that event you can do some activities, which is about to be repeated every defined clcok interval. With clock you will be having a control to start and stop at any moment.

As, post #6 ABG has control over fractal tree, when to start and when to stop, with speed also.

Basically, when you learn a new component in the Blocks Editor, learn the gold Event blocks and match them up with the blue command blocks that initiate long running processes that will eventually trigger those Events.

Thanks!

Hello I'm also from Arduino IOT coding backgrounds and i Know a bunch of coding languages including C++ I have a school project I know this reply is 2 years late I'm new to MIT app inventor I need to create a loop pls tell me clearly How

For loop:

int x = 1;

int main() {
    for (int i = 1; i <= 10; i++)
        x *= 5;
}

We have three statements here, int i = 1, i <= 10 and i++.

  • The loop initializes with the first statement, i = 1.
  • Then it runs through the loop, which multiplies x by 5.
  • It runs the third statement, incrementing i by 1.
  • It checks if i satisfies the second statement, which is a test of whether i is smaller than 10.
    • If it fails the test, terminate.
    • If it passes, repeat.

Essentially we run through the loop 10 - 1 + 1 = 10 times, each time increasing the value of i by one. The equivalent in App Inventor:

for-loop

Here the same thing happens. It creates a variable number = 1 (from), and each time, it multiplies x by 5, and increases number by one (by). It checks if the number is smaller or equal to 10 (to), and if it is, repeat.


For-list loop:

vector<int> numbers = {1, 2, 5, 4, 3};

int main() {
   int sum = 0;
   for (int &n : numbers)
       sum += n;
}

We are iterating over a vector (list/array in other langauges), each time increasing the sum variable by the element. Technically we can use the previous number loop:

vector<int> numbers = {1, 2, 5, 4, 3};

int main() {
   int len = numbers.size(), sum = 0;
   for (int i = 0; i < len; i++)
       sum += numbers[i];
}

But as you can see, we do not really need the index, we just need to add the element. Here, a for-list loop is more efficient.

for-list


For-dictionary loop

unordered_map<char, int> occurrences = {
   {'H', 1},
   {'E', 1},
   {'L', 2},
   {'O', 1}
};

int main() {
   int numberLetters = 0;
   for (const pair<char, int> &pair : occurrences)
       numberLetters += pair.second;
}

Here, pair.first is the key of the pair, and pair.second is the value. The key is the letter, and the value is the number of occurrences of the character.

for-dictionary

App Inventor doesn't really have a "char" data type. We use strings instead.


While loop:

C++ has a stack data structure, where it functions as an array but we are only allowed to insert/remove the last item.

stack<int> numbers = {3, 4, 2, 1};

void clearStack(stack<int> &target) {
    while (!target.empty())
        numbers.pop();
}

Here, the clearStack() function removes all elements in the stack. It removes the last item as long as the stack is not empty, or as long as there is a last item.

This is how a while-loop works:

  • Test the boolean statement (!target.empty()).
  • If the boolean statement is true, run the code in the loop and repeat this process.
  • If it is false, break out of the loop and never test again.

while-loop

However, it is not recommended to use while loops for large numbers of iterations. Unlike a terminal, the app can slow down and even crash.


There is also a type of loop C++ has while App Inventor doesn't, and you may have heard of it already. The do-while loop is similar to the while loop, but it runs the code inside, then tests the boolean statement. I won't go through it here because it's not really related.

If you have any questions feel free to ask.

A Clock Timer is the AI2 equivalent of a wait loop.