MATLAB Answers

0

Can not terminate execution with if/for loop

Asked by Ali Tawfik on 30 Aug 2019
Latest activity Commented on by Walter Roberson
on 31 Aug 2019
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 Comments

Sign in to comment.

3 Answers

Answer by dpb
on 30 Aug 2019

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 Comments

Hi,
Thanks for your prompt reply. Return terminate the loop, but without doing all the solution/iterations , I mean I wanna do x=[ 1 2 3] so I wanna 3 possible answers, It terminates so only one iteration is done , I need to do all the iterations, and also, check the conditions,
dpb
on 31 Aug 2019
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.

Sign in to comment.


Answer by Walter Roberson
on 31 Aug 2019

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 Comments

Hi Walter,
Thanks for your answer .
But maybe you misunderstand me,
I have 3 values of x, I wanna the loop of for do it's normal 3 iterations when d== f or b, and then execute otherwise, the loop till running, until I type d==f or b.
However, your solution is running (nested) does not terminate even by typing the correct condition of d==f or b.
Thanks,
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

Sign in to comment.


Answer by dpb
on 31 Aug 2019
Edited by dpb
on 31 Aug 2019

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 Comments

Sign in to comment.