我应该在嵌套for循环的第二个整数上使用哪些命名约定?

Gra*_*sby 3 naming-conventions

我对编程很新,我只是想知道在下面的例子中我在这段代码中使用的第二个整数的适当名称

for (int i = 0; i < 10; i++)
{
    for (int x = 0; x < 10; x++)
    {
    //stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

我通常只是将它命名为x,但我觉得这很快就会让人感到困惑.这种东西有标准名称吗?

sar*_*old 5

根据您要迭代的内容,上下文中的名称可能很容易或很明显:

for(struct mail *mail=inbox->start; mail ; mailid++) {
    for (struct attachment *att=mail->attachment[0]; att; att++) {
        /* work on all attachments on all mails */
    }
}
Run Code Online (Sandbox Code Playgroud)

对于那些情况i最有意义的一个外循环变量,约定使用j,k,l,等等.

但是当你开始嵌套时,要更加注重有意义的名字.你会在六个月内感谢自己.