Exiting Do while Loop using a key on the Keypad

Greetings everyone, im trying to exit a "Do...While loop using the "B" key on my keypad".

this is the snippet of code:

if(customKey == "A"){

do{
gasDetector();
soundDetector();
}while(customKey != "B")
}

I've also tried it with condition "while(customKey == "B")" to exit the loop but it does not work.
the key "B" will ask me to re-enter the password then choose to arm (turn gas sensor and sound sensor on)system or disarm(turn both sensors off). but it just stays inside the loop until i take power off the arduino or press the reset button on the board.

If you want a responsive program then don't use WHILE. Just allow loop() to do the repetition and then you can check the I/O pins on every iteration of loop(). Have a look at how it is done in Several Things at a Time

...R

Suggest you avoid ‘while/do’ like a plague.

At a minimum, wait until you understand that while/do can often make your code very choppy.

The advice you've had about avoiding do/while is good advice.

Your problem here in particular is that nothing inside the loop changes the value of customKey. So once you enter the loop, you never leave.

The value of customKey never changes while your do/while loop is running, so expecting the do/while to ever end is unrealistic. You need to call getKey() now and then, in the body of the do/while loop. Or, as advised, get rid of it.