Simple do while loop using while(true);

Many times in the examples of C programs, I came across these kind of loops. What do these kind of loops really do?

do {

    while (...) // Check some condition if it is true.
    { 
        calculation 1
    }

    // Some new condition is checked.

} while(true);

What is the need of while(true); Is it used for infinite looping? Can someone please explain what the above loop really does. I am new to C programming


These loops are used when one wants to loop forever and the breaking out condition from loop is not known. Certiain conditions are set inside the loop along with either break or return statements to come out of the loop. For example:

while(true){
    //run this code
    if(condition satisfies)
        break;    //return;
}

These loops are just like any other while loop with condition to stop the loop is in the body of the while loop otherwise it will run forever (which is never the intention of a part of the code until required so). It depends upon the logic of the programmer only what he/she wants to do.


是的,它被用于无限循环,在这种情况下,最佳做法是打破一个条件的外观

do {

    while () //check some condition if it is true
     { 
     calculation 1
    }

    //some  new condition is checked,if condition met then break out of loop


    } while(true);

In C all loops loop while a condition is true. So an explicit true in the condition really means "loop while true is true" and so loops forever.

链接地址: http://www.djcxy.com/p/69506.html

上一篇: 类“自我”是什么意思?

下一篇: 简单的做while循环while(true);