How to break out of an infinite 'while' loop

Hi, I have an experiment wherein subjects must press either the left, right or up arrow button. If they choose another keyboard button, an error message should come up reminding them of the restricted choice and they choose again. If they choose an arrow button then, the rest of the trial continues. However, if they again choose a keyboard button other than an arrow key, the error message appears again and they choose again and so on until they choose correctly. I have created an infinite ‘while’ loop which should be broken once the ‘if’ condition is met, however, it keeps crashing when the question mark appears, despite the next button press fulfilling the criteria:

number_tries = 1;
while number_tries < Inf
    if KbName(keyCode1) == "LeftArrow"
    elseif KbName(keyCode1) == "RightArrow"
    elseif KbName(keyCode1) == "UpArrow"
    break;
    else
        Screen('TextSize', window, 17);
        Screen('DrawText', window, 'Please only select an arrow key to indicate your choice',    screenXpixels*0.1, screenYpixels*0.5, white);
        Screen('Flip', window);
        WaitSecs(2.0);
        
        % Question mark
        Screen('TextSize', window, 120);
        Screen('DrawText', window, '?', screenXpixels*0.48, screenYpixels*0.45, white);
        Screen('Flip', window)
        
        % Timestamp
        questionmarkOnset1 = GetSecs();

        % Check the keyboard to see if a button has been pressed
        [secs1, keyCode1] = KbStrokeWait;
        
        number_tries = number_tries + 1;
    end
end

Any suggestions would be much appreciated. Thanks in advance!