answersLogoWhite

0


Best Answer

The condition for a while loop is tested at the start of the loop. It is tested at the end of the loop for a do-while loop.

The body of a do-while loop will always be executed at least once. Whereas for

a while loop if the condition is false to start with, the body of the loop is never executed.

User Avatar

Wiki User

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

Wiki User

11y ago

The loops are the same. The test is at the start of the loop so both are done zero or more times.

The main difference is that the for loop has a built in subscript. The while loop you have to create any subscripts that you need and also add the increment where needed.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

In C, for loops, while loops, and do while loops are control structures for repeating a sequence of code based on a condition. While loops and for loops are interchangeable and work top down meaning it is possible to skip their code entirely. do while loops check the condition at the bottom of the loop meaning they always run their code at least once. the syntax for a for loop is as follows

for(c = 0; string[c] != NULL; c++)

{

printf("%c", string[c]);

}

this segment of code would print the elements of the character array string until an element was equivalent to NULL, the end of file character found at the end of strings.

the first statement is typically used to set a counter to keep track of how many times you have gone through the loop, either incremented as in the case above or decremented. This could be left blank but the semi colon is still needed.

The line following the first semi colon sets the condition. The condition is evaluated as a Boolean expression, and the loop will continue to run its block of code until the condition is false . The condition must be followed by a semicolon.

The statement after the second semi colon is typically used to update the counter. This likewise can be left blank but the semicolon is still needed. Any loop that does not update itself in some way will usually result in an infinite loop and unless there is something else built into the code such as a break statement this will cause a run time error. This means program would compile correctly and then crash during the run time.

A while loop could also be used to write a completely equivalent code.

c = 0;

while(string[c] != NULL)

{

printf("%c", string[c]);

c++;

}

Note that the variable being used to count through the loop is declared out side the loop. While loops consist only of a condition, and will loop through the block of code within them until the condition is false, like all loops.

the do while equivalent would look like this

c = 0;

do

{

printf("%c", string[c]);

c++;

} while( string[c] != NULL);

if string[0] was a NULL character, this code would still run once printing a NULL character to the screen before checking the condition. If the first character was not a NULL character, then this would run identically to the other loops described.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The difference is that do while loop allows you to make at least one iteration, even if the condition for while is wrong. For loops does not allow you to do that.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What do you know about for loop and do while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


What are the components of loop?

a loop consist of data initialization;test condition;updation; example a for loop for(int a=1;a<5;a++) the loop will be executed 5 times four positives result and the last test condition will be failed and the loop will be exited there are many loops some of them are while loop,do...while loop,for loop,maybe more...... do while is an exit check loop and while and for are entry check loop.


What is counted pretest loop?

While loop is counted as pretested loop.

Related questions

What is a nested loop in java?

A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.


What are the similarities of while loop and a do while loop?

They both loop


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.


The while loop is a type of loop?

Is loop


What is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


Definition of do loop while in visual basic?

There are 3 type of loop 1 is for loop 2 is loop while 3 is loop untile


How do you convert a while loop to do-while loop?

A for loop is classified as an iteration statement. A simple example might be... For x = 1 To 10 'loop body Next x The same loop expressed as a while loop (classified as a control flow statement) could be... x = 0 Do While x < 11 'loop body x = x + 1 Loop .


Can you write switch statement with in while loop?

Yes. The same goes for for-loop and do-while-loop.


How do i do a while loop inside my current loop?

while(predicate1) { while(predicate2) { ... } }


Which is more good to use for loop or while loop?

The main difference comes into picture when you use continue with them i.e. for and while. In a while loop if continue is used before the increment of a variable is done it converts into a infinite loop. i=1; while(i<10) { /* do stuff */ if(i==6); continue; i++; } The above piece of code will turn into an infinite loop. for(i=1;i<10;i++) { /* do stuff */ if(i==6); continue; } In the above for loop the value of will be incremented once it attains the value 6. Therefore it will not turn into a infinite loop. Cheers. astvansh> 'for' statement takes the start condition, stop condition, and the increment. 'while' statement just takes the stop condition. One scenario (which i can think of) where while 'needs' to be used rather than a for, would be when the counter needs to be incremented/decremented conditionally... string[] arr = {"a", "b", "b", "c"}; int i = 0; while( i< 10) { // do something constructive if(arr[i] == "b") { i = i + 2; } else { i++; } } Cheers, Ajeesh Another main difference between the two: a 'for' loop is called a determinate loop, meaning that we usually use it when we know ahead of time how many iterations we want. The 'while' loop is an 'indeterminate' loop, because we usually use it in situations where we do not know the number of times the loop may iterate.


What are the components of loop?

a loop consist of data initialization;test condition;updation; example a for loop for(int a=1;a<5;a++) the loop will be executed 5 times four positives result and the last test condition will be failed and the loop will be exited there are many loops some of them are while loop,do...while loop,for loop,maybe more...... do while is an exit check loop and while and for are entry check loop.