0

For and while loop can be used anywhere in replace of do-while then why the C language has do-while loop, what is it's real use .

#include <stdio.h>
int main()
{
do{
statements; 
}
while(condition);
}
return 0;
  • 4
    The body always executes at least once, unlike the other loops – M.M Jun 01 '21 at 06:39
  • But I can use while loop by having different condition –  Jun 01 '21 at 06:40
  • A different, but sometimes more complicated, condition. – Some programmer dude Jun 01 '21 at 06:42
  • Do-while can be used everywhere instead of for and while loop. So what’s the point? – gnasher729 Jun 01 '21 at 06:44
  • 1
    @Prince By that reasoning, you don't need both `for` and `while` loops, since you can always replace one with the other. Even further, you don't need any loop statements at all, since you can achieve the same effect with `if` and `goto` statements. The point is that the builtin loop statements better match the situations that arise in practice. Granted, `do` ... `while` is the least commonly used, but it does have its uses. – Tom Karzes Jun 01 '21 at 06:45

1 Answers1

0

The main use of using a do while loop is that even if the condition that you pass is false it will execute the loop at least once.

While Loop: First condition then execute the code

Do While Loop: First execute the code then condition

codingwith3dv
  • 359
  • 3
  • 8