C programming Language 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

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  

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

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

 



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

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

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.