Return to input, how?

6 views (last 30 days)
Christian
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!

Answers (1)

Walter Roberson
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
Christian on 15 May 2013
Awesome, it worked, but how about if I'm loading a file, and the file doesn't exist?
Christian
Christian on 15 May 2013
Nevermind i found out :) try catch end

Sign in to comment.

Categories

Find more on Two y-axis in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!