Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C programming Language Do-while loop

Posted on December 15, 2016December 26, 2019

Table of Contents

  • C Programming Language Do-while loop
    • Do-while loop
      • Syntex
      • Flow diagram – do-while loop
      • How to work do-while loop

C Programming Language Do-while loop

In this tutorial, we will discuss the concept of C programming Language Do-while loop

In this post, we are going to learn how to use the do-while loop in C language

Do-while loop

What is the usage of the do-while  loop in C? you can understanding after reading this article .

generally in programming languages, looping statements used in programming language to executes of block of code until the perticular condition is satisfied.


C program has 3 looping statement
  • for loop
  • while loop
  • do-while loop
here we can clearly understand do-while loop in C language

Do-while loop

Syntex

 
do{
statement(s);
}
while(condition);

 

Flow diagram – do-while loop

C programming Language Do-while loop
Flow diagram

The do-while loop is similar to while loop but one main difference between while and do-while loop. The do-while loop is executed only once time before checking the condition part. So you will understand the do-while loop is executed at least once. 


How to work do-while loop


First,the do-while loop executes only once. then the text expression is evaluated, if the test expression
becomes true, the codes inside the loop body is executed again. The process goes on until the test expression is false when the test expression is false, the do-while loop is terminated
Ex
program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=1;
    do{
         printf(“%i”,i);
          i++;
    }
    while(i<=10);


    //printf(“Hello world!n”);
    return 0;

}

In the program, test expression returns true codes inside the loop body are executes until become condition is false, so output here  

C programming Language Do-while loop
Example

Te program displays natural numbars 1 to given number

 

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=1;
    do{
         printf(“%in”,i);
          i++;
    }
    while(i>=10);

    return 0;
}

When the above code is executed, it produces the following result

C programming Language Do-while loop
Example
  when the code is executed test expression returns false  codes inside the body is executed only once
Calculate the sum of natural numbers 1 to n using do-while loop
Program
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
     int sum=0;
    int n;
    cout << "Enter the number as you wish" << endl;
    cin>>n;
   int i=1;
   do{
    sum=sum+i;
    i++;
   }while(i<=n);
   cout<<"The sum of 1 to "<<n<<" : "<<sum;
   getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the number as you wish
100
The sum of 1 to 100: 5050

 



While Loop in Java            while Loop in C++            While Loop In C

Do-while Loop in Java          Do-while Loop C++           Do-while loop in C

For Loop in C++                For Loop in C               For loop in java

If condition C++               If condition in Java         If condition in C

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes