How to stop for loop when receive serial strings from serial port.

I am a newbie in programming and need some help with my program.
Background: I am using arduino mega 2560 connected with PC. I send command strings from C# to arduino. I'm able to create a string array to save those received strings, then arduino will scan through those strings to do cnc program.
Now problem: When arduino doing a cnc program, when I press a button on C# program,it will send string "stop\n" to arduino and arduino will break out of the loop to stop step motor.

#include <math.h>
#include <EEPROM.h>
#include <avr/interrupt.h>
#define ARRAYSIZE 100
String results[ARRAYSIZE];
int pre_lineNum = 0;
int flag_lineNum = 0;
int flag_manual = 0;
int flag_homming = 0;
int flag_reset = 0;
int flag_stopx = 0, flag_stopy = 0, flag_stopz = 0;
int lineNum = 0;

String inputString = "";
String commandString = "";
boolean inputStringComplete = false;
boolean isConnected = false;
int count = 0, count_val = 0;


int cmStrLen;

int flag_mode = 0;
int flag_g;
float flag_x, flag_y, flag_z, flag_f, flag_i, flag_j;
int flag_count = 0;
int stop_x = 0, stop_y = 0, stop_z = 0;

void setup() 
{
  Serial.begin(9600);
  //setup something
  

}
void getCommand()
{
  
  commandString = inputString;
  
}

void serialEvent()
{
  while (Serial.available())
  {
    char inChar = (char)Serial.read();
    inputString += inChar;
    count++;
    if(inChar ==  '\n')
    {
      inputStringComplete = true;  
    }
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void return_home()
{
  //do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void manualMode()
{
  //do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void getData()
{
  if(commandString.equals("START\n"))
  {
        
  }
  if(commandString.equals("STOP\n"))
  {
             
  }
  else if(commandString.equals("STOPX\n"))
  {
    
  }
  else if(commandString.equals("STOPY\n"))
  {
    
  }
  else if(commandString.equals("STOPZ\n"))
  {
    
  }       
  else
  {
    for(int i = 0; i < cmStrLen; i++)
    {
      if(commandString[i] == ('G')||commandString[i] == ('X')||commandString[i] == ('Y')||commandString[i] == ('Z')||commandString[i] == ('F')||commandString[i] == ('I')||commandString[i] == ('J')||commandString[i] == ('F')||commandString[i] == ('M')||commandString[i] == ('P'))
      {
        count1 = i;  
      }
      if(commandString[i] == (' ')|| i == (cmStrLen - 1))
      {
         count2 = i;
      }
      if(count2 > count1)
      {
        info++;
        temp = commandString.substring(count1,count2);
        if(temp.substring(0,1) == "G")
        {
          flag_g = temp.substring(1,count2).toInt();
        }
        if(temp.substring(0,1) == "X")
        {
          flag_x = temp.substring(1,count2).toFloat();                
        }       
        else if(temp.substring(0,1) == "Y")
        {
          flag_y = temp.substring(1,count2).toFloat();         
        }
        else if(temp.substring(0,1) == "Z")
        {
          flag_z = temp.substring(1,count2).toFloat();         
        }
        else if(temp.substring(0,1) == "I")
        {
          flag_i = temp.substring(1,count2).toFloat();        
        }    
        else if(temp.substring(0,1) == "J")
        {
          flag_j = temp.substring(1,count2).toFloat();       
        }
        else if(temp.substring(0,1) == "F")
        {
         flag_f = temp.substring(1,count2).toFloat();       
        }
        if (flag_g == 90)//absolute mode
        {
          flag_mode = 1;                 
        }
        else if (flag_g == 91)//relative mode
        {
          flag_mode = 2;
        }
        temp = "";
      }     
      if(i == (cmStrLen-1))
      {
        temp = "";
        count1 = 0;
        count2 =  0;
        doLine();
        doCircle();
      }
    }  
  }
  commandString = "";
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void doLine()
{
  //do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void doCircle()
{
  //do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void resetZero()
{
  //do something
}

void loop() 
{
  
  if(inputStringComplete)
  {
    inputStringComplete = false;
    results[lineNum] = inputString;  
    lineNum++;
    /* Send from C# at least 2 line for 1 command then, check the line at the end of each command  */
    if(inputString == "STOP\n")
    {
      flag_stopx = 1;
    }
    if(inputString == "Manual\n")
    {
      flag_manual = 1;
    }
    if (inputString == "M30\n")
    {
      flag_lineNum = 1;
    }
    if (inputString == "Home\n")
    {
      flag_homming = 1;
    }
    if (inputString == "Reset\n")
    {
      flag_reset = 1;
    }
    commandString = "";  
    inputString = "";
  }
  ///////////////////////////////////////
  if(flag_stopx == 1)
  {
    //I tried to stop step motor by doing something in here but it's not working
  }
  if(flag_reset == 1)
  {
    commandString = results[0];//Get the first line to do command
    cmStrLen = commandString.length();
    resetZero();
    lineNum = 0;
    flag_reset = 0;
  }
  if(flag_homming == 1)
  {
    commandString = results[0];//Get the first line to do command
    cmStrLen = commandString.length();
    return_home();
    lineNum = 0;
    flag_homming = 0;
  }
  if(flag_manual == 1)
  {
    commandString = results[0];//Get the first line to do command
    cmStrLen = commandString.length();
    flag_mode = 2;
    manualMode();
    getData();
    flag_manual = 0;
    lineNum = 0; 
  }
  if(flag_lineNum == 1)
  {
    for(int nums = 0; nums < lineNum; nums++)
    {
      commandString = results[nums];
      
      cmStrLen = commandString.length();
      getData();    
    }
    commandString = "";
    lineNum = 0;
    flag_lineNum = 0;
  }
  flag_mode = 0;//reset mode
}

You want to execute commands in the order that they arrive except for the special "stop" command that jumps the queue?

I'm thinking that perhaps a button would be better than a command.

Is this G-code? I see some G's. There are a number of good G-code programs available for the Arduino. Start with one of them. They might seem complex to start with but you're creating your own spaghetti code that will be more complex by the time you finish.

MorganS:
You want to execute commands in the order that they arrive except for the special "stop" command that jumps the queue?

I'm thinking that perhaps a button would be better than a command.

Is this G-code? I see some G's. There are a number of good G-code programs available for the Arduino. Start with one of them. They might seem complex to start with but you're creating your own spaghetti code that will be more complex by the time you finish.

I have tried other G-code interpreter program available for the Arduino like UniversalGcodeSender, I'm trying to write my own G-code Interpreter GUI on C# because it's my school project, then send strings to Arduino and I can run g-code program from those read string already, but I can not read string from the serial port while the motor is running g-code program(for loop).

So don't do that. Make loop() do both. If there's in incoming character, look at it. If there's a command to be executed then do that too.

nevermind96:
Now problem: When arduino doing a cnc program, when I press a button on C# program,it will send string "stop\n" to arduino and arduino will break out of the loop to stop step motor.

To achieve that your Arduino will need to check for a new message between each step. If you need accurately timed steps and a high step rate that may not be possible.

I have written a CNC program for my small lathe. The PC program interprets the GCode and sends instructions for one move at a time and the Arduino completes a move before checking for the data for the next move. Stopping the system can be achieved by NOT sending data for another move.

If there is need for an emergency stop then I just shut off the power to the stepper motors.

If you want the Arduino to receive data for several moves and store them in a queue and then be able to "jump" the queue to react to a STOP message then the amount of work to be done between steps will be that much greater.

...R