如何终止嵌套循环

时间:2018-08-11 07:03:34

标签: c embedded atmel 8051

在这里,我想打破嵌套循环。我正在使用break语句来这样做。代码已脱离for循环,但无法脱离无限while循环。当条件为真时,我想转到另一个名为resetClock()的函数。我已经尝试了所有可能的解决方案returngotobreak,但仍然面临着同样的问题。

 #define MUX_PORT  P3
 #define enable_int{EA=1;}
 #define disable_int{EA=0;}

 void timer0() interrupt 1 {
     TL0 = 0x33;
     TH0 = 0xF5;
     MUX_PORT = 0x00;
     dig1 = dig2 = dig3 = dig4 = 0;
     DATA_PORT = 0x00;
     if (dig_disp > 4)
         dig_disp = 0;
     dig_disp++;

     switch (dig_disp) {
     case 1:
         dig1 = 1;
         MUX_PORT = seg_hex[sec0];
         break;
     case 2:
         dig2 = 1;
         MUX_PORT = seg_hex[sec1];
         break;
     case 3:
         dig3 = 1;
         MUX_PORT = seg_hex[min0];
         break;
     case 4:
         dig4 = 1;
         MUX_PORT = seg_hex[min1];
         break;
     }
 }

 void msDelay() {
     unsigned int i;
     for (i = 0; i <= 8; i++) {
         TMOD = 0x10;
         TH1 = 0x4B;
         TL1 = 0xFD;
         TR1 = 1;
         while (TF1 == 0);
         TR1 = 0;
         TF1 = 0;
     }
 }

 void start_Clock() {
     unsigned int loop_break = 0;
     TMOD = 0x01;
     TH0 = 0xF5;
     TL0 = 0x33; //63293
     IE = 0x82;
     TR0 = 1;
     while (SW_SHIFT == 1) {
         for (min1 = 0; min1 < 6; min1++) {
             for (min0 = 0; min0 <= 9; min0++) {
                 for (sec1 = 0; sec1 < 6; sec1++) {
                     for (sec0 = 0; sec0 <= 9; sec0++) {
                         msDelay();
                         if (SW_SHIFT == 0) {
                             loop_break = 1;
                             disable_int;
                             MUX_PORT = 0xFF;
                             dig1 = dig2 = dig3 = dig4 = 0;
                             break;
                         }
                         if (min0 == 2 && min1 == 1) {
                             min0 = 1;
                             min1 = 0;
                             sec0 = 0;
                             sec1 = 0;
                             continue;
                         }
                     }
                     if (loop_break == 1) break;
                 }
                 if (loop_break == 1) break;
             }
             if (loop_break == 1) break;
         }
         if (loop_break == 1) {
             resetClock();
         }
     }
 }

 void main() {
     unsigned int i, j;
     sec0 = sec1 = min0 = min1 = 0;

     SW_SET = 1;
     SW_SHIFT = 1;

     for (i = 0; i <= 250; i++)
         for (j = 0; j <= 1257; j++);

     start_Clock();
 }

 void resetClock() {
     unsigned int i, j, k, l;
     i = j = k = l = 0;
     SW_SHIFT = 1;
     SW_SET = 1;

     if (SW_SHIFT == 0) {
         dig_disp++;
         if (dig_disp > 3)
             dig_disp = 0;
     }
     switch (dig_disp) {
     case 0:
         while (SW_SET == 0) {
             dig1 = 1;
             i++;
             sec0 = i;
             MUX_PORT = seg_hex[sec0];
             if (i == 9)
                 i = 0;
         }
         break;

     case 1:
         while (SW_SET == 0) {
             dig2 = 1;
             j++;
             sec1 = j;
             MUX_PORT = seg_hex[sec1];
             if (j == 5)
                 j = 0;
         }
         break;

     case 2:
         while (SW_SET == 0) {
             dig3 = 1;
             k++;
             min0 = k;
             MUX_PORT = seg_hex[min0];
             if (k == 9)
                 k = 0;
         }
         break;

     case 3:
         while (SW_SET == 0) {
             dig4 = 1;
             l++;
             min1 = l;
             MUX_PORT = seg_hex[min1];
             if (l == 1)
                 l = 0;
         }
         break;
     }
 }

4 个答案:

答案 0 :(得分:1)

while条件下,尝试添加另一个仍保留在true上的支票,只要您想在嵌套的for循环中继续。每当您设置要退出false循环的条件时,请确保此检查变为for

while((SW_SHIFT==1) && (cond_to_break_while == false)) {
for1() {
    for2() {
         ....
         forN() {
             cond_to_break_all_the_forloops = true;
             cond_to_break_while = true;
         }
         ....
    }
}
}

答案 1 :(得分:1)

goto的用法并不多,但是打破嵌套循环就是其中之一。

尝试这样的事情:

while (SW_SHIFT == 1) {
    for (min1 = 0; min1 < 6; min1++) {
        for (min0 = 0; min0 <= 9; min0++) {
            for (sec1 = 0; sec1 < 6; sec1++) {
                for (sec0 = 0; sec0 <= 9; sec0++) {
                    msDelay();
                    if (SW_SHIFT == 0) {
                        disable_int;
                        MUX_PORT = 0xFF;
                        dig1 = dig2 = dig3 = dig4 = 0;
                        goto myLabel;
                    }
                    if (min0 == 2 && min1 == 1) {
                        min0 = 1;
                        min1 = 0;
                        sec0 = 0;
                        sec1 = 0;
                        continue;
                    }
                }
            }
        }
    }
}

myLabel:
    resetClock();

当然,您可以将该条件添加到while条件中,例如:while (SW_SHIFT == 1 && loop_break == 0),但在我看来,使用goto看起来更干净。

答案 2 :(得分:0)

只需将所有计数器设置为其最终值,即可完成

while (SW_SHIFT == 1) {
     for (min1 = 0; min1 < 6; min1++) {
         for (min0 = 0; min0 <= 9; min0++) {
             for (sec1 = 0; sec1 < 6; sec1++) {
                 for (sec0 = 0; sec0 <= 9; sec0++) {
                     msDelay();
                     if (SW_SHIFT == 0) {
                         min1 = 6;
                         min0 = 9+1;
                         sec1 = 6;
                         sec0 = 9+1;
                     }
                     else if (min0 == 2 && min1 == 1) {
                         min1 = 0;
                         min0 = 1;
                         sec1 = 0;
                         sec0 = 0;
                     }
                 }
             }
         }
     }
 }

如果在内循环的其余部分周围使用break,则无需else

您的代码中的continue语句是多余的。

还使用96之类的“ 魔术数字”被认为是不正确的做法。考虑改用constenum#define s。

答案 3 :(得分:0)

以我认为最可读的方式编写代码的正确方法是使用函数并返回:

void start_Clock() {
     unsigned int loop_break = 0;
     TMOD = 0x01;
     TH0 = 0xF5;
     TL0 = 0x33; //63293
     IE = 0x82;
     TR0 = 1;

     delay(); // give this some meaningful function name

     resetClock();
 }

void delay (void)
{
  while (SW_SHIFT == 1) {
    for (min1 = 0; min1 < 6; min1++) {
      for (min0 = 0; min0 <= 9; min0++) {
        for (sec1 = 0; sec1 < 6; sec1++) {
          for (sec0 = 0; sec0 <= 9; sec0++) {
            msDelay();

            if (SW_SHIFT == 0) {
              disable_int; // NOTE: this looks fishy, likely bug
              MUX_PORT = 0xFF;
              dig1 = dig2 = dig3 = dig4 = 0;
              return ;
            }

            if (min0 == 2 && min1 == 1) {
              min0 = 1;
              min1 = 0;
              sec0 = 0;
              sec1 = 0;
            }

          } // for (sec0 = 0; sec0 <= 9; sec0++)
        } // for (sec1 = 0; sec1 < 6; sec1++)
      } // for (min0 = 0; min0 <= 9; min0++)
    } // for (min1 = 0; min1 < 6; min1++) 
  } // while (SW_SHIFT == 1)
}