C中的Double For Loop语法 [英] Double For Loop syntax in C

查看:188
本文介绍了C中的Double For Loop语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C新手,必须编写一些模拟给定函数的代码。但是,我很难理解代码中的第二个循环在做什么。对于(init; condition; increment){$ b $(

)),语法似乎没有遵循标准的循环语法: b声明; 



$ b $ p
$ b

下面是我正在检查的代码,这是循环的第二个,我是(i = 0; i <=(n1-n))不在线,我没有看到这个的在线版本

  n2); i ++){
count = 0; ($($))($($)
)打破;
}
else
count ++;
if(count == n2)
total ++;




$ b我假设这两个for循环在一个,因为有两个增量。我认为不平等与循环相似,但我不确定。令人困惑的是,似乎没有j循环的条件。我不知道这是一个语法快捷方式,还是有一个特殊的循环,我无法找到一个在线资源。

解决方案


我假设它是一个循环中的两个,因为有两个增量。

好吧,不是真的。对于循环来说,第二个也遵循规定的语法,对处理循环相关的变量只是一个小小的幻想。



对于一个循环语句,如($ = $,$,$,$,$,$, b $ b

可以解释为

 (j = i,k = 0);(k  ^^^^^^^^^^^ ^^^ ^^^ ^^^^^^^ 
init条件后循环体操作。

现在,初始化和post-body表达式块使用逗号运算符

相关引用 C11 ,第6.5.17节


逗号运算符的左操作数被评估为void表达式;在评估和右操作数的评估之间有一个
的顺序点。然后计算右边的
操作数;结果有它的类型和价值。

详细说明,相同的语法可以分解为

  j = i; //初始化,只有一次
for(k = 0; k //......loop body
j ++; //在主体中的最后一个表达式
} //循环结束


I am new to C and have to write some code that emulates a given function. However, I am having a difficult time understanding what the second for loop in this code is doing, explicitly. The syntax doesn't seem to follow standard for loop syntax of:

for ( init; condition; increment ) {
statement(s);
}

Here is the code I am examining and it's the second for loop that I am not following and I don't see any online version of this

for (i = 0; i <= (n1-n2); i++){   
    count=0;
    for(j = i,k = 0; k < n2; j++,k++){
        if (*(s1+j)!=*(s2+k)){
            break;
        }
        else
            count++;
        if(count==n2)    
            total++;                         
    }
}

I'm assuming it's two for loops in one, since there are two increments. And I think the inequality is similar to a 'while loop', but I'm not certain. The confusing piece is that there doesn't seem to be a condition for the j loop. I'm not sure if it's a syntax shortcut or if there is a special loop I can not find a resource for online.

解决方案

I'm assuming it's two for loops in one, since there are two increments.

Well, not really. The second for loop also follows the prescribed syntax, it's just a little fancy about handling the loop related variables.

For a loop statement like

 for(j = i,k = 0; k < n2; j++,k++){

can be interpreted as

 for( (j = i,k = 0)  ; (k < n2) ; (j++,k++) ){
       ^^^^^^^^^^^      ^^^^^^     ^^^^^^^  
       init            condition    post loop body operation.

Now, the initialization and post-body expression blocks makes use of comma operator.

Related, quoting C11, chapter §6.5.17

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

To elaborate, the same syntax can be broken down to

 j = i;                               //initialization, only once
 for( k = 0  ; k < n2 ; k++ ){        // good ol' syntax
  //......loop body
  j++;                                //last expression in the body.
 }                                    //end of loop

这篇关于C中的Double For Loop语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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