C

时间:2015-08-24 03:01:43

标签: c while-loop infinite-loop

我刚刚了解了bool。似乎boolwhile (true)while (1)都是相同的无穷功能。 boolwhile (true)while (1)更有优势吗?我看不出有任何区别。

BOOL:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(void) 
{
    bool keep_going = true;  // Could also be `bool keep_going = 1;`
    while(keep_going) 
    {
        printf("This will run as long as keep_going is true.\n");
        // keep_going = false;    
        // Could also be `keep_going = 0;`
    }

    printf("Stopping!\n");
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:4)

而(true)是无限循环。 Boolean是具有true和false值的数据类型。如果您想在任何阶段更改为false,则使用Bool。所以你可以改变bool里面for循环的值来停止循环。