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
John D'Errico 2023 年 2 月 23 日
編集済み: John D'Errico 2023 年 2 月 23 日

0 投票

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 ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 2 月 23 日

編集済み:

2023 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by