Never ending while-loop

2 views (last 30 days)
Nicle Davidson
Nicle Davidson on 11 Sep 2021
Commented: Nicle Davidson on 12 Sep 2021
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

Accepted Answer

DGM
DGM on 11 Sep 2021
Edited: DGM on 11 Sep 2021
There are a few problems. LW(1) will never match variabel, since they're different lengths. The first 3 characters of LW will never match variabel, since all that's going on here are circular shifts, and LW does not contain that substring.
pT='abc';
variabel = pT;
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
Pos=1;
while notFound == 1
%disp('we are in the while-loop');
if Pos == 1
% Pos will never be changed
if strcmp(LW(1:numel(variabel)),variabel) % need to test at least the same number of chars
%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');
% store=RW;
% for j=1:(length(RW)-1)
% RW(j)=RW(j+1);
% end
% RW(end)=store(1);
RW = circshift(RW,-1); % does the same thing as above
% store=MW;
% store(1)=MW(end);
% for m=1:(length(MW))
% store(m+1)=MW(m);
% end
% MW=store(1:end-1);
MW = circshift(MW,1); % does the same thing as above
% store=LW;
% for n=1:(length(LW)-1)
% LW(n)=LW(n+1);
% end
% LW(end)=store(1);
LW = circshift(LW,-1); % does the same thing as above
end
else
% all this can be simplified in a similar manner
end
end
Even if the substring under test is something like [LW(1) MW(1) RW(1)], that won't match variabel either, since LW and RW are shifted in the same direction and no occurrence of ['a' MW(1) 'c'] will ever occur.
As the comment mentions, the second part simplifies as well. Depending on what the actual goals are, the entire thing should simplify further yet. For example, if Pos always is initialized to 1, then the whole second part of the loop is unused and can be removed. This is because the only time Pos is set to 0 is when the loop exits.
pT='abc';
variabel = pT;
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
notFound = 1;
while notFound == 1
if strcmp(LW(1:numel(variabel)),variabel) % need to test at least the same number of chars
dW= append(dW, RW(1));
notFound = 0;
else
RW = circshift(RW,-1);
MW = circshift(MW,1);
LW = circshift(LW,-1);
end
end
But of course, I don't know what this is really supposed to be doing.
  4 Comments
DGM
DGM on 12 Sep 2021
Edited: DGM on 12 Sep 2021
Let's see if this gets us closer:
variabel = 'test';
LW= 'azyxwvutsrqponmlkjihgfedcb';
MW= 'acedfhgikjlnmoqprtsuwvxzyb';
RW= 'abcdefghijklmnopqrstuvwxyz';
dW='';
% 1 means LW; 0 means MW
% behavior should alternate between the two
Pos = 1;
for p=1:length(variabel)
% i inverted the logic here so the flag could be used for multiple things
isDone = false; % reset the flag inside the loop
while ~isDone
if Pos == 1
isDone = strcmp(LW(1),variabel(p));
else
isDone = strcmp(MW(1),variabel(p));
end
if isDone
fprintf('Finished in pos %d!!\n',Pos);
dW = append(dW, RW(1));
Pos = xor(Pos,1); % toggle
disp([RW; MW; LW])
else
fprintf('I turn the wheels one step (in pos %d)\n',Pos);
RW = circshift(RW,-1); % shift left (ccw)
MW = circshift(MW,1); % shift right (cw)
LW = circshift(LW,-1); % shift left (ccw)
end
end
end
I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1)
Finished in pos 1!!
hijklmnopqrstuvwxyzabcdefg uwvxzybacedfhgikjlnmoqprts tsrqponmlkjihgfedcbazyxwvu
I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0) I turn the wheels one step (in pos 0)
Finished in pos 0!!
yzabcdefghijklmnopqrstuvwx edfhgikjlnmoqprtsuwvxzybac cbazyxwvutsrqponmlkjihgfed
I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1) I turn the wheels one step (in pos 1)
Finished in pos 1!!
ijklmnopqrstuvwxyzabcdefgh suwvxzybacedfhgikjlnmoqprt srqponmlkjihgfedcbazyxwvut
I turn the wheels one step (in pos 0)
Finished in pos 0!!
jklmnopqrstuvwxyzabcdefghi tsuwvxzybacedfhgikjlnmoqpr rqponmlkjihgfedcbazyxwvuts
Nicle Davidson
Nicle Davidson on 12 Sep 2021
Thank you, your code writing is very good.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!