C language Changing For Loop to While loop

旧城冷巷雨未停 提交于 2021-02-05 11:53:41

问题


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

change


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..


回答1:


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

is functionally equivalent to

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


来源:https://stackoverflow.com/questions/64972464/c-language-changing-for-loop-to-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!