嵌套for循环递增1,但不进入循环块 [英] nested for loop incremenets by 1 but doesn't enter the loop block

查看:116
本文介绍了嵌套for循环递增1,但不进入循环块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小项目中有一个函数,这个函数包含一个嵌套循环,最外面的循环递增1它满足条件,但它执行一次循环体。如果i = 1,j = 1则x = 3且y = 3 - 我使用调试器确定这些值 - 但是我在没有执行的情况下变为2然后是3,只有当I = 1时才有效。

Hi, I have a function in a small project, This function contains a nested loop the outermost loop increments by 1 it satisfies the condition but it executes the loop body once. if i = 1 , j = 1 then x = 3 and y = 3 - I'm sure about these values using the debugger - but i becomes 2 then 3 without executing, it works only if I = 1.

bool is_available_in_box(const short& num,const short& box_index)
{
    short place = get_location(box_index);
    short i = place / 10 , j = place % 10;
    short x = i + 2 , y = j + 2;
    for (i = i; i <= x; i++)
    {
        for (j = j; j <= y; j++)
        {
            if ( game[GAME_BOARD][i][j] == num )
            {
                return false;
            }
        }
    }
    return true;
}





问题出在哪里?



谢谢,Samuel。



Where is the problem ??

Thanks, Samuel.

推荐答案

自我分配( i = i; j = j; )已如上所述 PIEBALDconsult ,没用。



无论如何,你的外环应该执行三次,并且对于每次这样的迭代内部循环应该执行三次

[update]

作为 PIEBALDconsult 指出 j = j 是一个错误(我忽略了它):在第一次外循环迭代时,它是无用但无害的。但是,由于第二次外部迭代, j = j 等同于 j = y + 1 ,从而阻止内部循环执行。

[/ update]

然而,在任何迭代中,如果条件

Self assignments (i=i; j=j;) as already noted by PIEBALDconsult, are useless.

In any case, your outer loop should be executed exactly three times, and for each of such iterations the inner loop should be executed exactly three times.
[update]
As PIEBALDconsult pointed out j=j is a mistake (I overlooked it): at first outer loop iteration it is useless but harmless. Since second outer iteration, however, j=j is equivalent to j=y+1, preventing inner loop execution.
[/update]
However, at any iteration, if the condition
game[GAME_BOARD][i][j] == num

为真,然后由于

return false;

声明。


这篇关于嵌套for循环递增1,但不进入循环块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆