How to start again from the previous step?

I want to do something like:
x = input('value of x:');
y = input('value of y:');
if (x>=y)
...
else
fprintf('undesirable');
% Then I want to return to the beginning and ask for new x and y inputs. Is there a way to do this? Thank you!

 Akzeptierte Antwort

Amit
Amit am 24 Jan. 2014
Bearbeitet: Amit am 24 Jan. 2014

0 Stimmen

4 Kommentare

Hi thx for the advice. I just created a for loop. Could you pls have a check, and if possible suggest better alternatives?
for i = 1:10
x = input('value of x:');
y = input('value of y:');
if (x>=y)
...;
break
else
fprintf('undesirable');
end
end
Another problem is that when 'undesirable' happens, inputs are asked again, but everything is lumped into one line like:
undesirablevalue of x:
Is there a way do separate the sentence into two lines?
Maybe add something after 'undesirable' like:
fprintf('undesirable\enter');
? What would be the correct command?
Amit
Amit am 24 Jan. 2014
Bearbeitet: Amit am 24 Jan. 2014
This code will work. To separate undesirable with the next one, you can use \n operator which is for new line. One problem with this code is that it will loop for only 10 times. If in 10 times, x does not become >= y, then also the code will exit. You want the loop to go on until its not undesirable. I think while loop would be better.
Try something like this:
x = 1;
y = 10;
while (x < y)
x = input('value of x:');
y = input('value of y:');
if (x < y)
fprintf('undesirable\n');
end
end
Thank you so much!:D
I was even thinking about using
for i=1:inf
to solve the issue you raised, but your while loop just avoids it nicely.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 24 Jan. 2014

Kommentiert:

am 24 Jan. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by