Need help to figure out why this loop does not stop running

I am running some code that contains the following lines of code. It keeps running and running without stopping. Can someone help me to figure out why?

do while K<=Ku;    
/* implied VAR(1) estimates */
    b_lr = b_r*(1-(rho^K)*(phi^K))/(1-rho*phi);
    b_ld = b_d*(1-(rho^K)*(phi^K))/(1-rho*phi);
    b_ldp = (rho^K)*(phi^K);
    ident = b_lr-b_ld+b_ldp;
    aux1 = -K*b_r*(rho^K)*(phi^(K-1))*(1-rho*phi)+rho*b_r*(1-(rho^K)*(phi^K));
    aux2 = -K*b_d*(rho^K)*(phi^(K-1))*(1-rho*phi)+rho*b_d*(1-(rho^K)*(phi^K));
    aux3 = (1-rho*phi)^2;
    aux_br = aux1/aux3;
    aux_bd = aux2/aux3;
    jacob = ((1-(rho^K)*(phi^K))/(1-rho*phi))~0~aux_br|0~((1-(rho^K)*(phi^K))/(1-rho*phi))~aux_bd|0~0~K*(rho^K)*(phi^(K-1));
    varcov = jacob*avcovb*jacob';
    tstat1 = (b_lr|b_ld|b_ldp)./sqrt(diag(varcov));
    tstat2 = ((b_lr-1)|(b_ld+1)|(b_ldp-1))./sqrt(diag(varcov));
    b_lrk[K,1] = b_lr;
    b_ldk[K,1] = b_ld;
    b_ldpk[K,1] = b_ldp;
    identk[K,1] = ident;
    t_lrk1[K,1] = tstat1[1,1];
    t_ldk1[K,1] = tstat1[2,1];
    t_ldpk1[K,1] = tstat1[3,1];
    t_lrk2[K,1] = tstat2[1,1];
    t_ldk2[K,1] = tstat2[2,1];
    t_ldpk2[K,1] = tstat2[3,1];

1 Answer


The loop in your questions starts out with:

do while K <= Ku;

this means that the loop will keep running until K is greater than Ku. The problem is that neither K, nor Ku change value during the loop. So if K starts the loop with a value less than Ku, it will forever have a value less than Ku. The code inside the loops needs a line to increment K. If you want to iterate over each integer from K to Ku + 1, then you should add the line:

K = K + 1;

just before the endo statement. Alternatively, if you changed the do while statement to a for loop, you would not have to add the K = K + 1 line. As an example, the loops below are equivalent:

k = 1;
ku = 20;
do while k <= ku;
    print k;
    k = k + 1;
k = 1;
ku = 20;
for k(1, ku, 1);
    print k;

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy