Help with return to nested loop

Hi, I need help with loops,
I have a while loop and in it I need to check input so I used nestef if
if the input is not valid it will print a msg and what I cannot seem to do is to send it beck to the beginning of the while loop to insert a different input
But whenever I try to print break or return at the end of the if, it just ends the whole function and not returns to the beggining of the loop

2 Comments

Show us the whole code ..
function spellMat = SpellStoring()
%the function will recive spells, check their validity and if they are
%valid, and if so it will add them to a metrix
i = 0;
x = 1;
while x==1 %will continue forever until an inside break
i = i+1; %row number
spell = input("Please enter a spell. to stop enter the word 'stop'",'s');
grade = str2double(spell(5:7)); %transfer the 5-7 varieble to one grade
if strcmp(spell,'stop') %check if writen stop
return
else %if not, continue to check validity
if isempty(intersect('HP RW HG',spell(1:2)))... %compare name to valid ones
|| isempty(intersect('ABCDEFG',spell(3)))... %compare grade to valid ones
|| isempty(intersect('IAC',spell(4)))... %compare spell to valid ones
|| grade <=100 && grade<= 000 || isempty(intersect('WSCN',spell(8)))
%check grade and tresure validity
disp('Invalid spell representation');
return
end
end
spellMat = zeros(i,8);
spellMat = [spell; spellMat];
end
end

Sign in to comment.

 Accepted Answer

Julian Schmid
Julian Schmid on 6 May 2020
Edited: Julian Schmid on 6 May 2020
From what I got you want to do something like that? It is easier to get an idea, if you provide the code you have written so far. The break or return statement does not end the if statement but the loop you are currently in.
inputArray = logical([1 0 1 0 0]); % can be strings and other numbers
nInput = length(inputArray);
ii = 1; % initialize index counter
while ii ~= nInput % nInput = number of arguments you want to check
if inputArray(ii) == false % or strcmp(inputArray(ii),'some condition') or ...
% input is wrong, display a message
disp('Something went wrong')
end
% increase counter
ii = ii + 1;
end

9 Comments

thank you but not quite what I was lookimg for
as you can see in the code that I have posted if the input is valid I want to add it to a matrix, and if not to ask for a different input
There is no code :-)
It was caught in the spam filter.
can you see it now?
Julian Schmid
Julian Schmid on 6 May 2020
Edited: Julian Schmid on 6 May 2020
yes working on it. It would be really helpful if you could provide a vaild spell so I can check the code. There seem to be a couple of odd things going on.
Sure, HPAI085N, RWBC100W
Ok, so maybe this is something you can work from. It is not ideal, because it does not preallocate spellArray but as long as there are not too many inputs provided by the user you should be fine.
Also instead of using isempty(intersect(...)) you should use the contains function e.g. contains(spell(1:2),{'HP RW HG'}). You dont catch inputs like RABC100W with the intersect function as it returns 'R' and not an empty output
function spellArray = SpellStoring()
% the function will recive spells, check their validity and if they are
% valid, and if so it will add them to a matrix
spellArray = [];
while 1 % will continue forever until an inside break
spell = input("Please enter a spell. to stop enter the word 'stop':",'s');
if strcmp(spell,'stop') % check if writen stop
% use break to exit the while loop
% use return to the function which called this function (parent function)
% the input provided by the user is returned in spellArray variable
break
elseif numel(spell) < 8
% check here to make sure the input has at least 8 characters
% otherwise we get an error when validating the parts of the
% input
% check grade and tresure validity
disp('Invalid spell representation');
else % if not, continue to check validity
grade = str2double(spell(5:7)); % transfer the 5-7 varieble to one grade
if ~contains(spell(1:2),{'HP','RW','HG'}) ... %compare name to valid ones
|| isempty(intersect('ABCDEFG',spell(3)))... %compare grade to valid ones
|| isempty(intersect('IAC',spell(4)))... %compare spell to valid ones
|| contains(spell(5:7),'000') ...
|| grade >= 100 ...
|| isempty(intersect('WSCN',spell(8)))
% check grade and tresure validity
disp('Invalid spell representation');
else % spell is valid so add it to the array
spellArray = [spell;spellArray];
end
end
end
end
Maya Harel
Maya Harel on 6 May 2020
Edited: Image Analyst on 8 May 2020
Thank you so much. It works!
Maya can you then "Accept this answer" to give Julian credit (reputation points) for helping you? And also to let others know it's solved and ou've moved on. Thanks in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!