关于for和while循环的问题 [英] question about for and while loop

查看:107
本文介绍了关于for和while循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到for循环被称为反控制循环。因为它有一个变量来控制迭代次数。







我的问题是为什么while循环不被称为反控制循环?

I read that for loop is called counter controlled loop.because it has a variable to control the number of iterations.



my question is that why while loop is not called counter controlled loop?

推荐答案

你的前提可能适用于类似BASIC的语言(和Fortran),但C语言并不正确。与简单迭代相比,C及其后代在控制 for 循环方面具有更大的灵活性。
Your premise may be true of BASIC-like languages (and Fortran), but it is not true of C-like languages. C and its offspring allow much more flexibility in how you control a for loop than simple iteration.


A'for'循环是计数器受控是因为当计数器达到其极限时循环结束。

当不再满足布尔条件时,'while'循环结束,这可能是计数器超出限制或循环中的某些内容本身如果检查条件并允许循环结束。



我经常用布尔变量写'while'循环; ex:while(bExit!= TRUE)

在循环代码中,我可能有各种条件允许bExit设置为true。
A 'for' loop is counter controlled because the loop ends when a counter reaches its limit.
A 'while' loop end when a boolean condition is no longer met, which may be that a counter has exceeded a limit or something in the loop itself if checking the condition and allowing the loop to end.

Frequently, I write 'while' loops with a boolean variable; ex: while (bExit != TRUE)
Within the loop code, I may have various conditions that will allow bExit to be set to true.


不仅蒂姆说,但(我的CPP有点生疏)你可以这样做:



Not only what Tim said, but (and my CPP is a little rusty) you can do something like this:

struct {
  int val1;
  int val2;
} myStruct;

myStruct[] myStructArray;

//Do stuff that initializes the structure

for (int *ptr = &myStructArray; ptr <= &myStructArray[lastElem]; ptr+=sizeof(myStruct))
{
   //Manipulate each item in the structure
}





可能无法编译,但你明白了。 For循环不一定使用计数器,它们使用迭代器。你可以用while循环做一些非常奇怪的事情:





May not be compilable, but you get the point. For loops don't necessarily use "counters", they use iterators. You can do some pretty strange stuff with while loops:

for (bool flag = false;flag;)
{
    //keep doing something until you flip flag
}







for (;;)
{
    //Infinite loop
}





Etc ... for循环只是一个初始化,条件和每次循环时运行的语句(通常用于迭代或计数。



所以写出for循环意味着什么:



(初始条件)直到(条件为真) )(对于身体)然后(增量变量)



其紧凑形式变为:





Etc... The for loop is just an initialization, condition, and a statement that runs every time it loops (which is typically used for iteration or counting.

So writing out what a "for" loop means:

for (initial condition) until (condition is true) do (for body) then (increment variable)

Which in its compact form, becomes:

for (initial condition; exit condition; increment variable)
{
 method body
}





因此它不称为反控制循环因为它不一定是由计数器控制的,它仍然是一个条件检查(奇怪的是,编译器将For循环转换为while循环,所以你可以使用while循环和循环一样,反之亦然)。



So its not called "counter controlled loop" because its not necessarily controlled by a counter, Its still a condition check (strangely enough, compilers turn For loops into while loops, so you can do the same with while loops as for loops and vice versa).


这篇关于关于for和while循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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