I need my code to loop while my input is binary (ones and zeros). once the user inputs any other value then the loop should stop.

bin = input('Input binary number: ','s');
index = 1;
while index >= length(bin) | index <= length(bin);
D = bin2dec(bin)
bin = input('Input binary number: ','s');
end
% this is my current code but it is not ending properly. it is ending because of an error rather then the loop ending

 채택된 답변

while true
bin = input('Input binary number: ','s');
if ~all(bin == '0' | bin == '1')
break
end
D = bin2dec(bin)
% ...
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 3월 10일

편집:

2023년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by