MATLAB: If i have a nested while loop and the second condition of the loop is true but first is wrong how to make it go back again to the first while loop

#nested#while#loops

x=input('enter k');
n=numel(x);
while n<3
x=input('enter correct number of k');
n=numel(x);
if n<3
continue;
else
while any(x<0)
x=input('enter positive numbers of k');
if any(x<0)
continue;
else
break;
end
end
end
end

Best Answer

You don't need two loops,
n = 0;
x=input('enter correct number of k');
while n<3
n=numel(x);
if n>=3 && all(x>0)
break;
elseif n>=3 && any(x<0)
x = input('please enter positive values');
n = 0;
continue;
else
x=input('enter correct number of k');
n = 0;
continue;
end
end
Related Question