how i can stop infinite loop in arduino

i hve code about intergrated keypad and arduino, i put random tone in one key with infinite loop, but i cannot stop that tone ,, please help me :frowning:
this is my code

#include <toneAC.h>
#include <Keypad.h>
int acak;
int vol=10;
char key1;
const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts 
byte colPins[COLS] = {6,7,8,12}; //connect to columnpinouts
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
 //Serial.begin(9600);
}
void loop()
{
  //int vol=10;
  char key = keypad.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      //this is what i want to stop
        case 'D':
       
       while(key!='0'){
        //key1 = keypad.getKey();
        acak=random(10000,19000);
        toneAC(acak,vol);
        delay(5000);
      
      }
        break;
        case '*':
        noToneAC() ;
        if(vol>1){
        vol=vol-1;
        }
        break;
        case '#':
        noToneAC() ;
        if(vol<10){
         vol=vol+1;
        }
        break;
        default:
     noToneAC() ;
        Serial.println(key);
    }
  }
}

while(key!='0'){
//key1 = keypad.getKey();
acak=random(10000,19000);
toneAC(acak,vol);
delay(5000);

when does key become 0? you already know its d, so why test for 0?

Where in the while loop will key ever become '0' to stop the loop ?

yeah i want '0' key to stop the loop but it didnt do anything

UKHeliBob gave you a hint. I won't be so subtle.

       while(key!='0'){
// read the keypad again here, and put the character in key
        key = keypad.getKey();
        acak=random(10000,19000);
        toneAC(acak,vol);
        delay(5000);