-1

i have the following code which runs infinitely when executed. it is fine if i remove the CONTINUE statement. however, with the CONTINUE statement the problem arises. the code is

Bn_x=zeros (length(pu_arrival), 1); Bn_x(1)=6;
Br_x=zeros (length(pu_arrival), 1); Br_x(1)=2;
jn1= zeros (length(pu_arrival), 1); jn1(1)=3;
in=zeros (length(pu_arrival), 1);
jn2= zeros (length(pu_arrival), 1); jn2(1)=3;
jr1=zeros (length(pu_arrival), 1);  jr1(1)=1;
ir=zeros (length(pu_arrival), 1); 
jr2=zeros (length(pu_arrival), 1);  jr2(1)=1;
numb_chan_idle_N=0;
numb_chan_idle_R=0;

for i=2:24 %length(pu_arrival)
    if rem(i,2)==0
        [Bn_x,Br_x]=failure3(numb_chan_idle_N,numb_chan_idle_R,in,jn1,jn2,ir,jr1,jr2,Bn_x,Br_x,i);
            continue
    end

end

%%%%%%%% The called function %%%%%%%%%%%%

function [Bn_x,Br_x] =failure3(numb_chan_idle_N,numb_chan_idle_R,in,jn1,jn2,ir,jr1,jr2,Bn_x,Br_x,i)
temp=0;
while temp<1
x=randi([1 6]);
if x==1
    if in(i-1)>0
        temp=temp+1;
    end
elseif x==2
    y=randi([1 2]);
    if y==1 
        if jn1(i-1)>0
            temp=temp+1;
        end
    elseif y==2 
        if jn2(i-1)>0
            temp=temp+1; 
        end
    end
elseif x==3  
    if ir(i-1)>0
    end

elseif x==4
    y=randi([1 2]);
    if y==1 
        if jr1(i-1)>0
            temp=temp+1; 
        end

    elseif y==2 
        if jr2(i-1)>0
            temp=temp+1; 
            jr2(i)=jr2(i-1)-1;
            Br_x(i)=Br_x(i-1)-1;
    else
        fprintf('JR2 destined to fail but it is already=%d\n', jr2(i-1))
        continue
        end
    end

elseif x==5
    if numb_chan_idle_N>0
        temp=temp+1; 
    end
elseif x==6
    if numb_chan_idle_R>0
        temp=temp+1; 
    end
end
end
end

I want controller to go back to the FOR loop after IF condition satisfies and its inner statement executes. However, the controller never comes out. I can't figure out what is wrong with the code.

Abdullah1
  • 47
  • 5
  • Are you saying that if you cut down `failure3` more you don't see the problem any more? You don't define `numb_chan_idle_N` or `numb_chan_idle_R`. – Cris Luengo Feb 26 '19 at 16:45
  • @CrisLuengo, Actually when I cut down more (failure3), the code gives error. That's why I retained this much of code for posting here. – Abdullah1 Feb 26 '19 at 16:50

1 Answers1

0

You are generating infinite loops inside fault3, the continue statement works as intended, the code just never reaches it.

In the code you given, there are only certain input dependent conditions that will cause temp=temp+1, the necessary condition to exit failure3. There are certain combinations of numbers (such as the ones you provided as example) that will never trigger any of the if conditions inside failure3, thus never exiting.

You can easily see this by adding the following to your code:

function [Bn_x,Br_x] =failure3(numb_chan_idle_N,numb_chan_idle_R,in,jn1,jn2,ir,jr1,jr2,Bn_x,Br_x,i)
temp=0;
iteration = 0
while temp<1

    iteration=iteration +1

    ...

The logic behind failure3 is broken. In any case, whenever you have an infinite loop while temp<1, try to add an extra statement such as while temp<1 && iteration<500 so you can debug the code better.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120