What is the difference between while and do while loop? Explain with examples.



What is the difference between while and do while loop? Explain with examples...

Answer / Sonika Dubey

While and do-while loops are both control structures used for repetition in C++. The main difference between them lies in their execution behavior.nn1) While Loop: The while loop checks the condition at the beginning of the loop. If the condition is true, then the body of the loop is executed; otherwise, the loop terminates immediately without executing the loop body.nnExample:n```cppnint i = 0;nwhile (i < 5) {n cout << i << endl;n i++;n}n```n2) Do-While Loop: The do-while loop checks the condition at the end of the loop. Regardless of whether the condition is true or false, the body of the do-while loop is executed at least once before checking the condition.nnExample:n```cppndo {n cout << i << endl;n i++;n} while (i < 5);n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Is it possible to use a new for the reallocation of pointers ?

1 Answers  


What is this pointer in c++?

2 Answers  


Write a short code using c++ to print out all odd number from 1 to 100 using a for loop

1 Answers  


What is a memory leak c++?

1 Answers  


Define upcasting.

1 Answers  


char *ch = "abcde"; char c[4]; how to copy 'ch' to 'c'?

4 Answers   Thomson Reuters,


What is the outcome of cout< a) 16 b) 17 c) 16.5

1 Answers  


What is the difference between the compiler and the preprocessor?

1 Answers  


Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; else return fn(v-1)+3; } for fn(7); a) 10 b) 11 c) 1

4 Answers   Quark,


What is a buffer c++?

1 Answers  


What are the total number of lines written by you in C/C++? What is the most complicated or valuable program written in C/C++?

2 Answers   Intel,


What is the use of setprecision in c++?

1 Answers  


Categories