C语言将For循环改为While循环 [英] C language Changing For Loop to While loop

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

问题描述

如何将for循环变成while循环
是否可以将array中的for循环改为while循环?

How to make for loop to while loop
Is it possible to change for loop in array to while loop?

改变


for(i = 0; i < 12; ++i) //for loop for selecting only positive
    {
        if(a[i] > 0) printf("%d ", a[i]); //display positive only using if statement
    }

to

int i = 0, a[12];
    while(i < 12)
    {
        if(a[i] > 0)
        {
            printf("%d ", a[i]);
        }
        i++;
    }

但如果更改,它不会显示与负数相同的正数..

but it doesn't display the positive same as the negative if changed..

推荐答案

int i;
for(i=0; i < count; i++)
{
    // Work item here
}

在功能上等同于

int i=0;
while (i < count)
{
    // Work item here
    i++;
}

这篇关于C语言将For循环改为While循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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