Question about basic program elements

Can someone please help me understand exactly how a program is processed.

as far as I can gather the program consists of three main elements.

Functions/statements run before the loop. My understanding is that these global statements are are run only once.
Functions/statements run in the loop which run continuously.
Functions/statements placed after the loop which only run if called from within the loop.

My question is this.
How can conditional Functions/statements be run before the loop. How can they pick up on changes occurring within the loop?

The reason for asking is that I'm making a menu using the MenuBackend library.
Almost all variables used by menu are called as global variables ie. before the loop, as is the function linking these variables together to form a menu structure. The only thing run from within the void loop() is the serial read of the wasd keystrokes used to navigate. All conditional serialPrinln() statements are called before the loop and should only be run once when powering up the program, yet they send updated info without problems. How?

I know I'm missing something obvious, but I'm just beginning to teach myself arduino and I need help figuring out what's going on here. :-?

Some kinds of statements can occur outside of a function. These include variable declaration and initialization (only when combined with the declaration) statements and function prototype declarations.

All other statements need to be inside a function.

The setup() function is called once.

The loop() function is called in an endless loop.

How can conditional Functions/statements be run before the loop.

They can't.

Functions can be defined before loop(), or after loop(), but the definition of the function is independent of the call to the function. That call can only occur inside a function.

Almost all variables used by menu are called as global variables ie. before the loop, as is the function linking these variables together to form a menu structure.

The variables are DEFINED as global variables, not called.

If you have specific questions about some code, post that code, and we'll try to answer the questions.

Thanks
What you are saying confirms that I have the basic understanding right.
My confusion seem to stem from me not having a clear enough grasp of the difference between functions, variables and data types.
Here are some code examples along with my assumptions. The examples occur in exact order top to bottom.

I don't understand exactly what the second line does.

#include <MenuBackend.h>
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);

I'm guessing this is where variables containing menu items are defined. Is this a function, if so how come there are no curly brackets? Also, how do I know which parts of this expression are variables I can alter and which are not?

MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
     MenuItem miView1 = MenuItem("Main View");
     MenuItem miView2 = MenuItem("Secondary View");
     MenuItem miSetup = MenuItem("SETUP");

I understand that this function sets up the relationships between the menu elements, and that it is run only once.

void menuSetup(){
     Serial.println("Setting up menu...");
     //add the Main View to the menu root
     //when add is used, as opposed to addX as you see below, the item is added below the item it's added to
     menu.getRoot().add(miView1); miView1.addRight(miSetup); miSetup.addRight(miView2);
     miView1.addLeft(miView2); miView2.addLeft(miSetup);
     }//END of menu function

This is the bit I'm having trouble understanding. This occurs before the loop.

void menuUseEvent(MenuUseEvent used){
     Serial.print("Menu use: ");
     Serial.println(used.item.getName());
     
     if (used.item == "SETUP") //comparison using a string literal
     {
           Serial.println("menuUseEvent found: SETUP");
     }

     if (used.item == "SETUP") //String comparison to set MenuUse behaviour 
     {
           Serial.println("Menu DOWN & RIGHT");
           rootMenu.moveDown();
           menu.moveRight();
     }
        
     //Copy the above and alter to expand menu
  }//END of menu use function

This is the function which prints changes to serial. This too occurs before loop.

void menuChangeEvent(MenuChangeEvent changed)
{
     //Serial.print("Menu change: ");
     //Serial.print(changed.from.getName());
     Serial.print("-->");
     Serial.println(changed.to.getName());
}

This is the setup()

void setup()
{
     Serial.begin(9600);
     
     menuSetup();
     Serial.println("Starting navigation (see source for description):")
}

Finally the loop. This is it, no references to any of the previous functions as far as I can tell.

void loop(){
           
      if (Serial.available()) {
      byte read = Serial.read();
            switch (read) 
                {
                  case 'w': menu.moveUp(); break;
                  case 's': menu.moveDown(); break;
                  case 'd': menu.moveRight(); break;
                  case 'a': menu.moveLeft(); break;
                  case 'e': menu.use(); break;
            }
}
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);

MenuBackend is a class. This statement is creating an instance of that class, called menu, by invoking the class constructor (which has the same name as the class), with two arguments.

     MenuItem miView1 = MenuItem("Main View");
     MenuItem miView2 = MenuItem("Secondary View");
     MenuItem miSetup = MenuItem("SETUP");

MenuItem is a class. There are three instances of the class being created.

I understand that this function sets up the relationships between the menu elements, and that it is run only once.

The function is being defined here. It is not being called. None of the code in this function is executed until the function is called from somewhere else.

The function menuUseEvent is being defined here. In the call to the constructor that created menu, the name of this function was passed into the constructor. When a specific event occurs, with respect to the menu, this function will be called. What event is that? I don't know. I'm not familiar with the class. The documentation for the class should define the event that will trigger a call to the function, though.

The function menuChangeEvent is similarly defined and called when an event occurs.

The setup() function calls the menuSetup() function that was discussed earlier. The menuSetup() function defines the hierarchy of the MenuItem instances that were created earlier.

Finally the loop. This is it, no references to any of the previous functions as far as I can tell.

When serial data arrives, it is read, and various members of the menu instances are invoked. When these functions are invoked, the callbacks (the functions defined earlier, whose names are passed to the constructor) get called. Which callbacks are made depends on the menu member being called.

Thanks for your thorough explanations. I am getting much closer to understanding it now.

Would it be reasonable to say that the reason I got confused is that the calls to the menu classes are handled by the library and therefore not explicit in the code for a beginner like me to see?

There is no documentation included with this library, so I'm moving ahead by trial and error, mostly error :-[

Would it be reasonable to say that the reason I got confused is that the calls to the menu classes are handled by the library and therefore not explicit in the code for a beginner like me to see?

As a beginner to C++, it can be hard to follow the logic, and hard to remember what an object is, and how methods for that object are called. It will become clearer over time.

Taking a C++ class, and learning to create C++ classes would be a big help.

Thanks for your patience.
I might have to do that. I was kinda hoping my limited knowledge of Java and PHP would help me move along, but my memory of it has faded a lot.

If anyone knows of any good online tutorials or lessons I would be very grateful.