What is Do-While Loop In C++

The do while loop is exactly same as while loop the difference is that statement execute.

first before check condition always at least one condition will execute if the condition is false.The loop is therefore guaranteed to execute at least once. The following code will print 0, as the condition will evaluate to false at the end of the first iteration:

int i =0;

 do

 { 

std::cout << i; ++i; // Increment counter 

} while (i < 0); 

std::cout << std::endl;

 // End of line; 0 is printed to the console

Do not forget the semicolon at the end of while(condition);, which is needed in the do-while construct.In contrast to the do-while loop, the following will not print anything, because the condition evaluates to false at the beginning of the first iteration:A while loop can be exited without the condition becoming false by using a break, goto, or return statement.

int i = 0;
do
{
std::cout << i; ++i; 

// Increment counter 

if (i > 5) { break; } 

} while (true); 

std::cout << std::endl;

 // End of line; 0 1 2 3 4 5 is printed to the console

Syntax Of Do While In C++

Initilization;
Do
{
Statement;
incr/decr;
}while(condition);

Note: Semicolon must be included after while in do while loop.Print Codeverb.com message for 5 times by using do while loop.

#include<iostream>
using namespace std;
int main(){
int i=1;
clrscr();
do
{
cout<<“Codeverb.com %dn”,i;
i++;
}while(i<=5);
return0
}

Output:
Codeverb.com 1
Codeverb.com 2
Codeverb.com 3
Codeverb.com 4
Codeverb.com 5

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Do While Loop In Cpp

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password