MATLAB: Code is too long

for loopif statementwhile loop

a = 4;
b = input ('2+2=? ')
if b == a
'ok'
else
b = input ('retry ')
if b == a
'ok'
else
b = input ('retry ')
if b == a
'ok'
else 'NOK'
end
end
end

Best Answer

I think you want
a = 4;
b = 0;
while b ~= a
b = input ('2+2=? ');
if b == a
break;
end
fprintf('Incorrect. Try again.\n');
end
fprintf('Correct.\n');