answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

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!

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How many while statements are possible in do..while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What is the difference between while dowhile?

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.


List out the differences between while and dowhile statement?

The do ..while loop is executed at least once, whereas the while loop may not be executed even once.


What is unique about a DOWHILE loop?

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.


Is a DOWHILE loop more flexible than a dountil loop?

No, they are equivalient. DO something WHILE condition; does the same thing as DO something UNTIL NOT condition;


What is difference between while and dowhile?

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


What is a dowhile statement in C programming?

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


Why while loop devoloped while for loop already present?

These statements are equal parts of the language, none of them if the 'better' or the 'older'.


What is main different between do while and while?

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


What are the statements that appear between the while and the end while clauses called?

Body of the loop


Does The statements within a while loop execute at least once?

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.


Compare Do-While with While Statements?

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.


What are control structures used in javascript?

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.