简体   繁体   English

造成无限循环的支架

[英]Brackets causing Infinite Loop

The brackets associated with following the three For loops cause the function to loop infinitely, 与跟随三个For循环相关的方括号使函数无限循环,

void Bubblesort (char ulist[27], char slist[27], int n)
{
int i,j;
char temp;
for (i=1;i<=n;i++)
{
    slist[i] = ulist[i];
    for (j=1; j <= n-1 ;j++)
    {
        for (i = 1; i <=n-j;i++)
        {     
            if (slist[i] < slist[i+1])
            {

                temp = slist[i];
                slist[i] = slist[i+1];
                slist[i+1] = temp;
            }

            }
        }
    }
}

While without the brackets; 虽然没有括号; the function reaches the end. 功能到达终点。

void Bubblesort (char ulist[27], char slist[27], int n)
{
int i,j;
char temp;
for (i=1;i<=n;i++)
    slist[i] = ulist[i];
    for (j=1; j <= n-1 ;j++)
        for (i = 1; i <=n-j;i++)
            if (slist[i] < slist[i+1])
            {
                cout << "Step 4";
                temp = slist[i];
                slist[i] = slist[i+1];
                slist[i+1] = temp;
            }



}

Would someone be able to explain the reason for this? 有人可以解释原因吗?

You have changed the semantics of the loop and your indents are misleading. 您已经更改了循环的语义,并且缩进具有误导性。

void Bubblesort (char ulist[27], char slist[27], int n)
{
    int i,j;
    char temp;
    for (i=1;i<=n;i++)
        slist[i] = ulist[i];
        for (j=1; j <= n-1 ;j++)
            for (i = 1; i <=n-j;i++)
                if (slist[i] < slist[i+1])
                {
                    cout << "Step 4";
                    temp = slist[i];
                    slist[i] = slist[i+1];
                    slist[i+1] = temp;
                }
}

should be indented as 应该缩进为

void Bubblesort (char ulist[27], char slist[27], int n)
{
    int i,j;
    char temp;
    for (i=1;i<=n;i++)
        slist[i] = ulist[i];

    for (j=1; j <= n-1 ;j++)
        for (i = 1; i <=n-j;i++)
            if (slist[i] < slist[i+1])
            {
                cout << "Step 4";
                temp = slist[i];
                slist[i] = slist[i+1];
                slist[i+1] = temp;
            }
}

The problem is that without the brackets you don't actually have nested for loops. 问题在于,没有括号,您实际上并没有嵌套for循环。 consider 考虑

for (i=1;i<=n;i++)
    slist[i] = ulist[i];
    for (j=1; j <= n-1 ;j++)

Without brackets only the slist[i] = ... line executes as part of the first for loop. 如果没有括号,则slist[i] = ...行将作为第一个for循环的一部分执行。 The second for loop executes as a completely separate entity 第二个for循环作为完全独立的实体执行

Once the loops are grouped together though the outer most and inner most loops are using the same index variable. 一旦循环被组合在一起,尽管最外面和最里面的循环都使用相同的索引变量。 Their mutual changes to the value conspire to create an infinite loop. 他们对价值的相互改变共同创造了一个无限循环。 Try using a different index variable for each for loop and it will fix the problem. 尝试为每个for循环使用不同的索引变量,它将解决此问题。 This works without brackets because the initial error I described causes the first and last loop to not be nested hence they're not manipulating the same value at the same time 这没有括号就可以工作,因为我描述的初始错误导致第一个和最后一个循环不嵌套,因此它们不会同时操纵相同的值

The two functions have different algorithms. 这两个函数具有不同的算法。 In the first function you have three nested loops. 在第一个函数中,您具有三个嵌套循环。 And the third loop uses the same control variablle 'i' as the first loop. 第三个循环使用与第一个循环相同的控制变量'i'。 So the loops are infinite because after the third loop i is always equal to 2. 因此循环是无限的,因为在第三个循环之后,我始终等于2。

In the second function the first loop is a separate loop that is the other two loops are not nested inside the first loop. 在第二个函数中,第一个循环是一个单独的循环,其他两个循环没有嵌套在第一个循环中。 So changing control variable 'i' in the third loop does not influence to the first loop. 因此,在第三个循环中更改控制变量“ i”不会影响第一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM