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
回答(1 个)
John D'Errico
2023-2-23
编辑:John D'Errico
2023-2-23
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!