How to Exit a for loop

Hi everyone,
Is there a simple way to exit a specific for loop? if I use break it exits all of the loops, I’ve also tried “return” but I think it did the same thing.
I’ll explain more: it’s a car controlled by bluetooth. when I press ‘1’ (forward on the android), the Arduino activate a function called “forward()”, when I release the button it’s sends ‘0’ to the arduino and activate the function “stopm()”.
In the function forward() the speed goes up slowly inside of a for loop (to accelerate gradulay) and write it to the motors. And here is the problem - inside the for loop there is a condition that if button is release break the for loop, and what suppose to happen is that the plan will go back to the main loop, and go to the “else” and call the function “stopm()”
here is the relevant part of the code:

void forward(){
  SpeedR=1500;
      if(Serial.available() > 0){   
            data = Serial.read();
      }
      
      for (SpeedL=1500;SpeedL<1700;SpeedL++){
        victorL.writeMicroseconds(SpeedL);
        victorR.writeMicroseconds(SpeedR);   
        delay(15);
        Serial.println(SpeedL); 
        SpeedR--; 

        if(Serial.available() > 0){   
          data = Serial.read();
          Serial.print(data);
          if(data!='1' && data!='5' && data!='8'){ ///////////////////////////  STOP FORWARD
          stopm();
          break;
        }

  }
  
 }
}



//////////////////////////////////////////////////    MAIN LOOP

void loop()
{
  if(Serial.available() > 0){  // Send data only when you receive data:
    data = Serial.read();      //Read the incoming data and store it into variable data
    Serial.print(data);        //Print Value inside data in Serial monitor
    Serial.print("\n"); 
    

    if(data == '1')  {
      forward();
    }
    else if(data == '3')  {
      back();
    }
        
    else if(data == '4')  {
      left();
    }
    else if(data == '2')  {
      right();
    }
    else if(data == '5')  {
      
    }
        
    else if(data == '6')  {
      
    }
    else if(data == '7')  {
      
    }
    else if(data == '8')  {
      
    }    
   else{
      
      stopm();
  
  
      }
  
  } 


                               
}

The problem is not the ‘for’ loop, or the ‘break’ statement, or the ‘return’ statement. The problem is your logic in ‘loop()’. If the condition ‘(data == ‘1’)’ is true, you will call (and eventually return from) the ‘forward()’ function. But you will never execute any of the ‘else’ clauses because your original condition was true.

void forward(){
  SpeedR=1500;
      if(Serial.available() > 0){   
            data = Serial.read();
      }

What is the purpose of the if statement, and block of code, when you don't use the value in data?

You need to rethink your entire approach. There should be ONE place where serial data is read and processed.

If the serial data is supposed to make the car stop (or stop turning), do that. If the data is supposed to make the car start, call forward(). But, forward() should do one thing, quickly, and return. To achieve the full speed, forward() will, obviously, need to be called many times.

In the end, there will be no for loops and no delay() calls in your code.

gfvalvo:
The problem is not the ‘for’ loop, or the ‘break’ statement, or the ‘return’ statement. The problem is your logic in ‘loop()’. If the condition ‘(data == '1')’ is true, you will call (and eventually return from) the ‘forward()’ function. But you will never execute any of the ‘else’ clauses because your original condition was true.

The if statement in the forward() function COULD take care of that. If the function were written properly. But, it isn't, and the for loop IS a problem.

PaulS:
But, forward() should do one thing, quickly, and return. To achieve the full speed, forward() will, obviously, need to be called many times.

In the end, there will be no for loops and no delay() calls in your code.

So if I call many times, maybe the solution will be to put in the main Loop -

while (data=='1'){forward;}

Will that be OK ?

while (data=='1'){forward;}

As soon as you enter that you can never leave. Nothing inside the while loop ever changes the value of data, so if data is one and you go into that loop you're stuck.

Quit all the looping. The loop function loops already. Don't think of writing a function to do a whole long sequence of things. Write a function that takes one small step and returns and let loop call it over and over to get all the steps done. That way the other things in loop get done too.