如何将这个“ for”循环转换为“ while”循环?

1990年

我需要将此for小循环转换为while循环,我该怎么做?

void count_the_numbers(int array[]){
printf("\n");
int i = -1;
while(i < 4){
    int count = 1;
    i++;

    for(int j=0; j < 5; j++){
        if(i != j && array[i] == array[j])
            count++;
    }
    printf("Number %d is %d times in this array/e\n", array[i], 
    conteggio);
}
}

这是另一个循环,给我的结果与上一个不同

约瑟夫·西布尔-恢复莫妮卡

for回路转换为while回路纯粹是机械的。给定任何这样的循环:

for(w; x; y) {
    z;
}

可以直接将其转换为:

w;
while(x) {
    z;
    y;
}

唯一的复杂因素(在您的示例中不适用)是,如果for循环continue在身体的任何地方使用,则需要y在每次出现之前将其复制到。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章