differnce between do and do while

Answer Posted / ravi kumar

The difference between "do while" and "do until" is that a
"do while" loops while the test case is true, whereas "do
until" loops UNTIL the test case is true (which is
equivalent to looping while the test case is false).


The difference between a "do ...while" loop and a "while {}
" loop is that the while loop tests its condition before
execution of the contents of the loop begins; the "do" loop
tests its condition after it's been executed at least once.
As noted above, if the test condition is false as the while
loop is entered the block of code is never executed. Since
the condition is tested at the bottom of a do loop, its
block of code is always executed at least once.

To further clear your concept on this, understand the syntax
and description of the two loop types:

while
The while loop is used to execute a block of code as long as
some condition is true. If the condition is false from the
start the block of code is not executed at al. The while
loop tests the condition before it's executed so sometimes
the loop may never be executed if initially the condition is
not met. Its syntax is as follows.

while (tested condition is satisfied)
{
block of code
}

In all constructs, curly braces should only be used if the
construct is to execute more than one line of code. The
above program executes only one line of code so it not
really necessary (same rules apply to if...else constructs)
but you can use it to make the program seem more
understandable or readable.

Here is a simple example of the use of the while loop. This
program counts from 1 to 100.


#include <stdio.h>

int main(void)
{

int count = 1;

while (count <= 100)
{
printf("%d\n",count);
count += 1; // Notice this statement
}

return 0;

}

Note that no semi-colons ( ; ) are to be used after the
while (condition) statement. These loops are very useful
because the condition is tested before execution begins.
However i never seem to like these loops as they are not as
clear to read as the do ...while loops. The while loop is
the favorite amongst most programmers but as for me, i
definitely prefer the do ...while loop.

do ....while
The do loop also executes a block of code as long as a
condition is satisfied.

Again, The difference between a "do ...while" loop and a
"while {} " loop is that the while loop tests its condition
before execution of the contents of the loop begins; the
"do" loop tests its condition after it's been executed at
least once. As noted above, if the test condition is false
as the while loop is entered the block of code is never
executed. Since the condition is tested at the bottom of a
do loop, its block of code is always executed at least once.

Some people don't like these loops because it is always
executed at least once. When i ask them "so what?", they
normally reply that the loop executes even if the data is
incorrect. Basically because the loop is always executed, it
will execute no matter what value or type of data is
supposed to be required. The "do ....while" loops syntax is
as follows

do
{
block of code
} while (condition is satisfied);


Note that a semi-colon ( ; ) must be used at the end of the
do ...while loop. This semi-colon is needed because it
instructs whether the while (condition) statement is the
beginning of a while loop or the end of a do ...while loop.
Here is an example of the use of a do loop.

include <stdio.h>

int main(void)
{

int value, r_digit; printf("Enter a number to be
reversed.\n");
scanf("%d", &value); do
{
r_digit = value % 10;
printf("%d", r_digit);
value = value / 10;
} while (value != 0); printf("\n"); return 0;


}

Read more:
http://wiki.answers.com/Q/What_is_the_difference_between_do_while_and_do_until_loop_in_c_programing#ixzz1Uh1xNxCw

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is C language ?

1511


I need testPalindrome and removeSpace #include #define SIZE 256 /* function prototype */ /* test if the chars in the range of [left, right] of array is a palindrome */ int testPalindrome( char array[], int left, int right ); /* remove the space in the src array and copy it over to the "copy" array */ /* set the number of chars in the "copy" array to the location that cnt points t */ void removeSpace(char src[], char copy[], int *cnt); int main( void ) { char c; /* temporarily holds keyboard input */ char string[ SIZE ]; /* original string */ char copy[ SIZE ]; /* copy of string without spaces */ int count = 0; /* length of string */ int copyCount; /* length of copy */ printf( "Enter a sentence:\n" ); /* get sentence to test from user */ while ( ( c = getchar() ) != '\n' && count < SIZE ) { string[ count++ ] = c; } /* end while */ string[ count ] = '\0'; /* terminate string */ /* make a copy of string without spaces */ removeSpace(string, copy, ©Count); /* print whether or not the sentence is a palindrome */ if ( testPalindrome( copy, 0, copyCount - 1 ) ) { printf( "\"%s\" is a palindrome\n", string ); } /* end if */ else { printf( "\"%s\" is not a palindrome\n", string ); } /* end else */ return 0; /* indicate successful termination */ } /* end main */ void removeSpace(char src[], char copy[], int *cnt) { } int testPalindrome( char array[], int left, int right ) { }

2193


How do I create a directory? How do I remove a directory (and its contents)?

595


What do you mean by c?

570


what do you mean by inline function in C?

603






What is the process to generate random numbers in c programming language?

594


What language is lisp written in?

599


What is keyword in c?

582


Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.

813


Why cant I open a file by its explicit path?

581


What is file in c language?

566


hi friends how r u as soon in satyam my interview is start but i m very confusued ta wat i do plz help me frndz wat can i do plz tell me some question and answers related with "C" which r asked in the interview .

1888


What is huge pointer in c?

569


Which one would you prefer - a macro or a function?

586


#include { printf("Hello"); } how compile time affects when we add additional header file .

1412