If you are asking how many statements can be a in while loop, there is no maximum. However, from a practical perspective you need to limit the number of statements to a small number to keep the logic understandable.
Different C versions (C88, C89) specify the minimumamount of nesting for DO, WHILE, FOR loops as well as IF, SWITCH and other nested blocks to comply with the standard. However, the maximum amount of nesting supported by a compiler depends on the actual compiler implementation. When being executed, the nesting limits depend on memory allocations and run time implementationsfor your environment.
However, what is possible is not the same as what is practical. Just because you can have 127 nestings of WHILE, IF, FOR and DO does not mean it makes sense to do so. A program doesn't just have to work as you wrote it. You may be able to make a nesting level of 5 or 10 readable by indentation, but indenting 20 times just won't work for most people. The program source has to be understandable by you a year later and by others even sooner if they have to do maintenance. That is why we have these concepts of smaller code segmens and modular programs.
A do...while loop is a statement in its own right so there can only be one while in a do...while loop. However, the body of the loop is not actually part of the do...while statement, it is the statement or compound statement that is being controlled by the do...while statement, but is otherwise a separate entity. As such, you can have as many while statements as you like "nested" within the body of a do...while loop.
However, nested loops are best avoided as they are often a poor design choice that only serve to hinder readability (and what is difficult to read is often difficult to maintain). Consider the following "spaghetti code" function:
void f (int x, const int y, const int z) {
do {
// ...
int ty {y};
while (--ty) {
// ...
int tz {z};
while (--tz) {
// ...
}
// ...
}
// ...
} while (--x);
}
I've omitted the actual implementation as we're only interested in the loop statements. Even so, it isn't immediately obvious that there is a while loop nested inside a while loop nested inside a do...while loop (three loops in total). The introduction of two temporary variables (ty and tz) simply adds to the confusion, but they are nonetheless necessary as the y and z variables control the inner loops.
Indentation and whitespace would obviously help us visualise the overall structure better, but if we simply refactored the function as three separate functions, we could replace the explicit temporaries with implicit ones and the logic would become that much easier to digest:
void fouter (int z) {
while (--z) { // ... } }
void fmiddle (int y, const int z) {
while (--y) { // ... fouter (z); // ... } }
void f (int x, const int y, const int z) {
do {
// ...
fmiddle (y, z);
// ...
} while (--x);
}
This is a vast improvement which comes at no additional cost. Although function calls can be expensive, even the worst compiler in the world is perfectly capable of inline expanding the fouter and fmiddle functions to reproduce the original f function and thus factor out the extra function calls entirely. It makes no sense for us to manually write "spaghetti code" when the compiler is perfectly capable of doing that for us Behind the Scenes. After all, that's its job!
If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed
Body of the loop
No. If the loop condition is not satisfied then the loop would not be executed even once. while(condition) { ..... /statements ..... } here,when the condition is true then the statements will be executed. Otherwise they would be skipped without being executed. Only for do.. while loops this execution at least once holds good.
A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.
The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.
While: If we can use while statement it will check the condition then proceed further loop statement.DoWhile: If we use dowhile, first execute loop statement then check the condition.
The do ..while loop is executed at least once, whereas the while loop may not be executed even once.
A DO-WHILE loop will always execute at least one iteration of the loop body. This is because the condition that controls the loop comes at the end of the loop, rather than at the beginning as per a WHILE or FOR loop.
No, they are equivalient. DO something WHILE condition; does the same thing as DO something UNTIL NOT condition;
the test condition will be checked first after wards the body of the loop will be excuted in while statement and the the do while statement represented the body of the loop will be executed first and then the test condition will checked next
"do statement while (...);" is a loop which does at least one iteration even if the condition after while is false. When, for instance, "while(...) statement" does not iterate at all if the condition after while is false.
These statements are equal parts of the language, none of them if the 'better' or the 'older'.
If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed
Body of the loop
No. If the loop condition is not satisfied then the loop would not be executed even once. while(condition) { ..... /statements ..... } here,when the condition is true then the statements will be executed. Otherwise they would be skipped without being executed. Only for do.. while loops this execution at least once holds good.
A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.
The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.