Using Loops & C++ for audio plugins

Instead of creating two topics I have a two part question.

I just got to the while loop section and it’s starting to make sense. What are some real world examples of using these loops? Any specific types of programs I’ve most likely used that have these or other loop types? Maybe once I get to the other types of loops this may come together for me. Just trying to figure out some examples of when we would need to repeat a section of code.

On a different note: I’m learning C++ so I can learn how to develop audio plugins, so I’m spending however many years it takes to get to that point. Does anyone know of any beginner/intermediate level books or courses that may cover this? Any particular types of functions I should be aware of when it comes to plugin development? I bought a book called designing audio effect plugins in C++ but it’s VERY advanced. But as with anything, starting simple and mastering the basics is important so I’m not skipping ahead on anything. Thanks in advance!

Lets say we have a giant list of contact information. The information you are getting in this list per contact is a name and a gender. Now a customer want’s this information in two documents. One document for female contact’s and another document of male contacts.
These are millions of contact’s so it would be impossible to do by hand.

But, to recognize if someone is male or female is very simple. you just have to do it over and over again.
Instead of creating an if statement for every one of the million contacts to check if they are male/female we just loop trough all the data and check the data while it loops over it.

I don’t know c++ so here is an example in pseudo code:

for every contact in the contactlist:   // <-- the loop
    if contact is male:
        add contact to male.document;
    if contact is female:
        add contact to female.document;

As you can see its way less work to simply write this than to check every data entry yourself.

eventually all a loop does is give you the ability to get every data entry separately which in turn allows you to put a check on or use this data.

When i use a loop i know the following things:

  • the checks i perform are always the same for all data entries i loop over.
  • the amount of data that needs to be checked could change.

There could be more reasons to use a loop tho. It is just a tool like you use a knife to cut bread. When you are creating code and you feel like your doing the same thing over and over. You could probably use a loop in this instance.

Hope it helps. Don’t have an answer for your second question.

2 Likes

Quick simple example of why you might use a while loop. Say you have a list of thousands of songs, and you want to randomly choose 20 of them for a playlist:

#include <iostream>;
#include <vector>;

int main() {
  std::vector<std::string> playlist;
  
  while (playlist.size() < 20) {
    //logic to randomly choose a song, and add it to the playlist vector
    //this code block will repeat until you have 20 songs in the vector
  }

  return 0;
}

Happy coding!

1 Like