Some code indentation and moving common code outside the IF branches:
initialspeed = 300;
sequenceArray = [12345,54321,12435,51234,42315]; % compare the input to this array
k = 1; % loop variable
sequence = zeros(1,length(sequenceArray)); % store the input here
prompt = 'Type in participants answer:';
while k < length(sequenceArray)+1
sequence(k) = input(prompt);
if sequence(k) ~= sequenceArray(k)
initialspeed = initialspeed + 50;
else
k = k+1;
sequence(k) = input(prompt);
if sequence(k) ~= sequenceArray(k)
initialspeed = initialspeed + 50;
else
k = k+1;
sequence(k) = input(prompt);
if sequence(k) ~= sequenceArray(k)
initialspeed = initialspeed + 50;
else
initialspeed = initialspeed - 40;
end
end
end
disp(initialspeed)
k = k+1;
end
You are prompted for further inputs even if the length is actually reached, because there are up to 3 input commands inside the loop.
The intention of the code is hard to recognize. I boldly guess, that you want something else, most of all because you are surprised, when the prompts appear more often than you expect.