How to know when a for loop is done without knowing how many iterations there will be?

136 views Asked by At

using a for-loop in javascript i´m getting a not known amount of id´s, they´re not in an array but coming one by one.

is there a way to get an alert when there are no more id´s to retrieve meaning the for loop is done?

i can´t wrap my head around this, any help would be greatly appreciated.

thanks!

edited with code for clarification.

    function iterateDevices(api) {

        var count = api.getcount("devices");     var apiPath = dequotePath(api);

            for (var i = 0; i < count; i++) {

            var deviceApi = new LiveAPI(apiPath + " devices " + i); if (deviceApi) {
            var deviceName = deviceApi.get("name"); var deviceid = deviceApi.id; //var
            deviceName = deviceApi.get("parameters"); var className =
            deviceApi.get("class_name"); var deviceApiPath = dequotePath(deviceApi);
            var chainsCount; var chainApi; var j;

                if ((className == "DrumGroupDevice") || (className ==
                "AudioEffectGroupDevice") || (className == "InstrumentGroupDevice")){
                //post(deviceName + " id " + deviceid + "\'\n"); //outlet(0,deviceid);
                //  arr.push(deviceName);

                    if (deviceApi.get("can_have_chains") == 1) { chainsCount =
                    deviceApi.getcount("chains"); // only racks have chains for (j = 0; j
                    < chainsCount; j++) { //    post("id" + deviceid + " found device " +
                    deviceName + " at path \'" + deviceApiPath + "\'\n");
                    //outlet(0,deviceid); chainApi = new LiveAPI(deviceApiPath + " chains
                    " + j); iterateDevices(chainApi);

                        myFunction(); } chainsCount = deviceApi.getcount("return_chains");
                        // only racks have chains for (j = 0; j < chainsCount; j++) {
                        //post("2 found device " + deviceName + "id"+deviceid + " at path
                        \'" + deviceApiPath + "\'\n"); //   outlet(0,deviceid); chainApi = new
                        LiveAPI(deviceApiPath + " return_chains " + j);
                        iterateDevices(chainApi);

                }
            }
            }
        }
    }

} iterateDevices.local = 1;
2

There are 2 answers

0
AudioBubble On

The purpose of a for loop is to deal with a known number of iterations. If you want to deal with an unknown number of iterations, you would use a while loop.

Of course, this is programming, so lets look at the crazy things we can do:

  1. Iterate over a collection. We dont necessarily know how many things are in the collection, but we may want to iterate over all of them. The number of things in the collection might even change as we're iterating.
  2. We can change how we iterate through the loop. That whole i++ part? What if we replace it with i += 2? Or what if it's i -=1? Or i += j and j changes while we're iterating?
  3. We can change how we break out of the loop. You can put a break statement in there to break out of the loop anytime. You can also change the conditional of the loop. What if i < 100 is replaced by i<j? Or what if we replace it with i<100 || q == true?
0
Marco On

You may use a while loop instead of a for and insert a condition to terminate the loop.

Using pseudo-code, you could do something like:

other_ids = True // boolean var
while(other_ids) {
    // do what you have to do
    other_ids = FuncionToCheckWhetherThereAreNewIds
}

FuncionToCheckWhetherThereAreNewIds should be a function that gives you true if there are new ids and false if there are not.