Never ending while-loop
Ältere Kommentare anzeigen
I have a while-loop that I can not get out of.
What I get out of my test is that:
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
we are in the while-loop
I turn the wheels one step-Pos == 1
etc.
It shows that my code goes into the while-loop, where it says 'we are in the while-loop', and then it goes into the if statement, checks to which position we are, then it do the strcmp, and if true goes into it, if not goes to else, it should however make the Pos variable 0 when has been to Pos 1 and vice versa.
howerver it goes into the if for Pos 1 and strcmp somehow is false and it goes to else, and it does the same again.
could I stop this while loop and do what is the goal with it?
notFound is used to stop this while looping, but maybe I have put it wrong place?
*I need to go first to Pos 1 test and second time to Pos 0, and change from 0 to 1 againg and back to 0 next time. (interchangeable)
variabel = 'test';
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
Pos=1;
for p=1:length(variabel)
while notFound == 1
disp('we are in the while-loop');
if Pos == 1
if strcmp(LW(1),variabel(p))
disp('Is in pos 1!!\n');
dW= append(dW, RW(1));
notFound = 0;
Pos=0;
else
disp('I turn the wheels one step-Pos == 1');
RW = circshift(RW,-1); % does the same thing as above
disp(RW);
%counterclockwise
MW = circshift(MW,1); % does the same thing as above
disp(LW);
%clockwise
LW = circshift(LW,-1); % does the same thing as above
disp(LW);
end
else
if strcmp(MW(1),variabel(p))
disp('is in pos 0!!\n');
dW= append(dW, RW(1));
notFound = 0;
Pos=1;
else
disp('I turn the wheels one step-Pos == 0');
RW = circshift(RW,-1); % does the same thing as above
disp(RW);
%counterclockwise
MW = circshift(MW,1); % does the same thing as above
disp(LW);
%clockwise
LW = circshift(LW,-1); % does the same thing as above
disp(LW);
end
end
end
end
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!