Code flow for precission applications

This is a question about programming. Mainly about the loop() function

As an example Consider code where you get data from an accelerometer, and if the Gs are high enough you do something.

Consider the following pseudocode:

void setup()
{
pinMode(1, INPUT);
bool flag = false;
}

void loop
{
int x= analogRead(1);
if (x>2)   //if the Gs we get from accelerometer
  flag = true;
if (button)  // if user hits the emergency button
  flag = true;

if (flag == true)
{
//GET SERIAL DATA (EG GPS COORDINATES)
//DO SOME HEAVY PROCESSING
}
}

Now loop runs repeatedly That means all the commands are executed in a continuous/serial style

Since that happens, what will happen if the accelerometer reading where (x>2), which is what we look for, happen in the REAL world, at a time where in the CPU, the cpu is at that time processing or checking following commands in the program flow, like

if (button)  // if user hits the emergency button
      flag = true;

    if (flag == true)
    {
    //GET SERIAL DATA (EG GPS COORDINATES)
    //DO SOME HEAVY PROCESSING
    }

Is there any chance that the cpu will miss real world events, because at that time the real world event occured, the cpu was processing other code. Or rather, not checking for that at that time. For our example, when the event occured, it wasnt checking the analogread() at that time, but was processing following code.

Should i worry about that?

Is there any chance that the cpu will miss real world events, because at that time the real world event occured, the cpu was processing other code.

Yes, there is. So, don't write blocking code.

On the other hand, consider the duration of these "real-world" events. How long will this "event" last? If the duration of the event is in months, it's unlikely that you will miss it. If the duration is in nanoseconds, you probably will.

Excuse me but what is block code?

Is there a way to write better code that ensures less loss of events?
In the previous example i mentioned, a good way to deal with it would be only to check the acceleromemter values, and the two ifs (if accel is bigger than threshold) and (if emergency button is pressed).

While it might not work in this case, an interrupt can also make sure that events like this are handled. Can your accelerometer be programmed to send an interrupt if there is a large change in acceleration?

nikolakos:
Excuse me but what is block code?

Blocking code is code that stops and waits (blocks) for something to happen. See the 'blink' sketch for an example. This is easy to code, but very limited in its ability to handle multiple things.

Non-blocking code is code that doesn't stop and wait. See the 'blink without delay' sketch for an example.