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

do-while loop in C++ programming language

Posted on July 19, 2016May 16, 2020

Table of Contents

  •  do-while loop in C++ programming language
    • How do-while loops work
    • Do while loop Example

 do-while loop in C++ programming language

In this tutorial, we will discuss do-while loop C++ programming language.

The do while loop is functioning similar while loop but there is a small difference. The body of the do while loop is executed at least once before the test expression is evaluated.

Syntax

do{
//codes inside the body of loop
}while(testExpression);

Flow diagram

do while loop in C++ programming language
flow diagram

How do-while loops work

  • The body of the do while loop is executed once only
  • Then the test (boolean) expression is evaluated
  • If the test expression is true the codes inside the body of the loop are executed.
  • This process continues until the test expression becomes false
  • finnally, when the test expression is false the control exits from the loop

Do while loop Example

Program 1

This program displays natural numbers from 1 to n using do while loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i=1;
    while(i<=10){
    cout << i << endl;
    i++;
    }
    getch();
    return 0;
}

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

1
2
3
4
5
6
7
8
9
10

 

Program 2

This program allows the user to enter some numbers then it uses to find the sum of given numbers until the user enters zero

#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);

cout<<"Total sum = "<<sum;

return 0;
}

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

Program 3

This program allows the user to enter the number then it uses to find the factorial of given numbers

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,fact=1,i=1;

    cout << "Enter the number" << endl;
    cin>>num;
  do{
    fact=fact*i;
    i++;
  }
    while(i<=num);
    cout<<"The factorial of "<<num<<" : "<<fact;
    getch();
    return 0;
}

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

Enter the number
5
The factorial of 5: 120
Suggested post
while Loop in C++     C++ Array
for Loop C++      C++ Class and Object
For Loop in Java
While Loop in Java
Do while Loop in Java
For Loop in C
While Loop In C
If condition 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