While loop that stops if certain number is inserted

I'm stuck trying to code a while loop that stops given a certain number. I'm a complete beginner at matlab, but i have searched the community and youtube and managed, until now.
This part I get to work:
while true a==1,2,3,4,6,7,8,9,10,
x=a+1;
end
ans=x
and I do get it to stop and wait for input by
while true a==1,2,3,4,6,7,8,9,10,
input('I stop when given the number 5: ',x) % by adding this line
x=a+1;
end
ans=x
BUT if i try to make it stop when given the number 5 I cannot for the life of me get it to work properly.
while true a==1,2,3,4,6,7,8,9,10,
input('I stop when given the number 5: ',x)
if x==5;
break
end
x=a+1;
end
ans=x

Respuestas (1)

John D'Errico
John D'Errico el 23 de Feb. de 2023
Editada: John D'Errico el 23 de Feb. de 2023
You should read the help for while. What you wrote was not even close to valid MATLAB syntax. Look at the examples found in the help docs.
For example, what might this do?
x = 0;
while x ~= 5
Then inside the while loop, use input to bring in a new value for x.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 23 de Feb. de 2023

Editada:

el 23 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by