Azzera filtri
Azzera filtri

How to start again from the previous step?

1 visualizzazione (ultimi 30 giorni)
Larry
Larry il 24 Gen 2014
Commentato: Larry il 24 Gen 2014
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!

Risposta accettata

Amit
Amit il 24 Gen 2014
Modificato: Amit il 24 Gen 2014
  4 Commenti
Amit
Amit il 24 Gen 2014
Modificato: Amit il 24 Gen 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
Larry
Larry il 24 Gen 2014
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by