Stuck in while loop inside an if statement

Good day! I have a trouble in my program. Basically, what I wanted it to happen was when I type in a certain word in the Serial Monitor, the RGB strip light would light up that certain color or command. So I used a series of if statements. My problem is that I also wanted to put in color swirl but when I use an if statement in only goes through the cycle once. So, I changed it and put it in a while statement. So now, my real problem is that when I put in the color swirl it doesn't stop even if I type in another command in the serial monitor. I have researched and said that I should use break? to stop the loop? But I don't know where in my program should I put it. I attached my whole program below. But this is where I got a problem. Thanks in advance!

while (command.equals("color swirl")){
   
        //blue to violet
        for (r=0;r<256;r++){
        analogWrite(red, r);
        delay(fade);
        }

        //violet to red
        for (b=255;b>0;b--){
        analogWrite(blue, b);
        delay(fade);   
        }

        //red to yellow
        for (g=0;g<255;g++){
        analogWrite(green, g);
        delay(fade);    
        }
 
        //yellow to green
        for (r=255;r>0;r--){
        analogWrite(red, r);
        delay(fade);    
        }

        //green to teal
        for (b=0;b<255;b++){
        analogWrite(blue, b);
        delay(fade);    
        }
  
       //teal to blue
       for (g=255;g>0;g--){
       analogWrite(green, g);
       delay(fade);
          }   
          
       if (!command.equals("color swirl")){
       !command.equals("color swirl");
       break;
          }    
       }

RGB_whole_program_serial.ino (2.3 KB)

Please include the complete program in your Post so we don't have to download it.

Generally speaking a WHILE loop blocks the Arduino until it completes - i.e. until the value that is tested in the WHILE changes.

It is better programming practice to use a variable to keep track of progress - for example it might increment once for each swirl. Then use an IF to test whether the required number has been completed. Allow loop() to do the repetition so that it can check for a new command.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

What Robin said, and in addition, you will not achieve much while you have delays in your code, delay means nothing else is happening or can happen. The tutorial Robin linked to plus Using millis for timing will help you, and this is a regular topic on here so some searching will come up with loads of help.

While and delay are both enemies of good, responsive code.