While循环在一个函数调用中执行两次

时间:2019-04-14 16:13:28

标签: c++ windows forms while-loop

我在while循环中遇到问题,我已指示key1变量仅应在至少执行了一些“ if”指令后才执行,但是,即使变量key1为,它也会重新执行while循环。等于0。 应该注意的是,我不会在函数的另一部分中更改key1变量的值

void form_table(...) {

int key1=1;

while (key1 != 0)  <==(2)
{
    key1=0;                 
    if (dx->get_numero() == 1 && dy->get_numero() == 1)
    {
     key1++;

    //Some code
    }

    else if (dx->get_numero() == 1 && dy->get_numero() == 0)
       {
        key1++;
        //some code     
       }
    else if (dx->get_numero() == 0 && dy->get_numero() == 1)
       {
           key1++;
               //some code
       } <==(1)
    else
       break;//I put it in case, but even with that it goes back into the buble while

}


}//Here is the problem, when finish the funcion execution the program comes back to the line (1), and then re-runs the while cycle (2)

1 个答案:

答案 0 :(得分:1)

如果将key1递增,则key1不同于0,则循环起作用,因此循环重新启动时,它不同于0。

您必须做:

while (key1 == 0) {
     // rest of the code
}