Can not terminate execution with if/for loop
조회 수: 8 (최근 30일)
이전 댓글 표시
I am trying to terminate code but not working, I am using for loop,
All I wanna is to break if (d) equal to the values written otherwise, wanna type d again, I mean wanna go up again to type (d)
Thanks in advance
clear all;
clc;
x=input('enter x values: ')
while 1
d=input('enter names','s')
for i=1:length(x)
if d=='f'
y(i)=x(i)+23
break %% I wanna terminate here:: does not terminate
elseif d=='b'
y(i)=x(i)+25
break %% I wanna terminate here:: does not terminate
else
display('go again for enter d correct name ')
continue %% I wanna go up to the line of typing :: d again
end
end
end
댓글 수: 0
답변 (3개)
dpb
2019년 8월 30일
Your break statement is in a nested for loop that is inside the while construct. break only exits the loop construct in which it resides--to quit the entire function/script, use return instead of break
댓글 수: 2
dpb
2019년 8월 31일
편집: Walter Roberson
2019년 8월 31일
I think you need to back up and explain the overall objective here instead.
If there's no intent to terminate the loop until the loop count is finished, then there is no need to use break at all; simply run the loop and take the path of action requested by the user on each iteration.
Walter Roberson
2019년 8월 31일
while 1
d=input('enter names','s')
for i=1:length(x)
if d=='f'
y(i)=x(i)+23
continue %% do the next x
elseif d=='b'
y(i)=x(i)+25
continue %% do the next x
else
display('go again for enter d correct name ')
break %% done with the x values, go back to d prompt
end
end
end
댓글 수: 2
Walter Roberson
2019년 8월 31일
while true
d = input('enter name', 's');
if ismember(d, {'f', 'b'})
break;
end
disp('go again for enter d correct name');
end
if strcmp(d, 'f')
y = x + 23;
elseif strcmp(d, 'b')
y = x + 25;
else
error('how did I get here?')
end
dpb
2019년 8월 31일
편집: dpb
2019년 8월 31일
What's the point of needing to type anything else after the loop has completed its three iterations? At that point you've already got solutions for the possible elements of the array to address...unless you want to be able to re-address a particular element more than once?
In that case, by far the easiest way would be to have a separate code to terminate the while instead of trying to make double use of the keys--
prompt='enter names '; % initial prompt
complete=false; % flag to have completed loop at least once
while 1
for i=1:length(x)
d=input(prompt,'s');
if d=='f'
y(i)=x(i)+23
elseif d=='b'
y(i)=x(i)+25
else
display('go again for enter d correct name ')
end
end
complete=true; % have gotten through the loop once
prompt='enter names ("x" to eXit)'; % done the full loop so tell user how to quit
d=input(prompt,'s');
if complete & d=='x', break,end % been thru once (at least) and wants to quit
end
break works above because now is not inside nested loop construct but outer loop.
Note that it also now will not allow the user to quit before the number of loop counter is done other than by Ctrl-C--once on the track, they've committed to ride until the train reaches the station.
Now the question is whether it is mandatory the user enter one of the allowable options or not...if that is required (not above nor in your initial code nor problem definition), then you should test the input and accept/reject the user response at that point instead.
If there is such a short list of possible answers, perhaps using a check box or dropdown list might be alternative instead of typing--makes the possible selection required to be in the allowable universe automagically.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!