In the previous post, we covered for loop and while loop, and in this post, we are going to discuss the do...while loop which is a variant of the while loop.
C++ do...while Loop.
do { // code block } while (condition);
//C++ Example of do while loop #include<iostream> using namespace std; int main(){ int i = 1; //do while loop do{ cout<<"Do While Loop Code Block"; }while(i <= 0); return 0; }
Do While Loop Code Block
//C++ Example of do while loop to print 5 number #include<iostream> using namespace std; int main(){ int i = 1, num; cout<<"Enter a value: "; cin>>num; //do while loop do{ cout<<num<<" x "<<i<<" = "<<num * i<<endl; i++; }while(i <= 10); return 0; }
Enter a value: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50Warning: Never write a condition for your loop that will be always evaluated as true else your loop will keep on running for infinite times until your memory gets full. (alert-warning)






Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.