BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – do while loop in C programming with example

By Chaitanya Singh | Filed Under: c-programming

In the previous tutorial we learned while loop in C. A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the condition. On the other hand in the while loop, first the condition is checked and then the statements in while loop are executed. So you can say that if a condition is false at the first place then the do while would run once, however the while loop would not run at all.

C – do..while loop

Syntax of do-while loop

do
{
    //Statements 

}while(condition test);

Flow diagram of do while loop

C do while loop

Example of do while loop

#include <stdio.h>
int main()
{
	int j=0;
	do
	{
		printf("Value of variable j is: %d\n", j);
		j++;
	}while (j<=3);
	return 0;
}

Output:

Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3

While vs do..while loop in C

Using while loop:

#include <stdio.h>
int main()
{
    int i=0;
    while(i==1)
    {
	printf("while vs do-while");
    }
    printf("Out of loop");
}

Output:

Out of loop

Same example using do-while loop

#include <stdio.h>
int main()
{
   int i=0;
   do
   {
	printf("while vs do-while\n");
   }while(i==1);
   printf("Out of loop");
}

Output:

while vs do-while
Out of loop

Explanation: As I mentioned in the beginning of this guide that do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop.

❮ PreviousNext ❯

Comments

  1. pilot says

    December 1, 2016 at 11:00 AM

    It would be better if there were some programs for practice so that we ourselves can try it

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap