Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Return to input, how?

Asked by Christian on 15 May 2013

I'm trying to create an input form that takes two different answers. And when you type a wrong answer, I want it to go back to the input and start over.

My code is (the original text was in danish, so i put a bad english example in the forms):

value = input('Write hi or goodbye','s');
  if isequal(value, 'hi')
      fprintf('Hello\n');
  elseif isequal(value, 'goodbye')
      fprintf('see you\n');
  else
      fprintf('wrong answer try again!\n');
  end

So what can i do about the else statement? I was thinking about some message saying: The answer is incorrect, will you try again? (y/n)

Thanks in advance!

0 Comments

Christian

Products

1 Answer

Answer by Walter Roberson on 15 May 2013
while true
  value = ...
  if ....
     break;
  else
     wrong answer
  end
end

Notice that there is no "break" for the case where you want it to loop back.

2 Comments

Christian on 15 May 2013

Awesome, it worked, but how about if I'm loading a file, and the file doesn't exist?

Christian on 15 May 2013

Nevermind i found out :) try catch end

Walter Roberson

Contact us