do...while Loops in C++
By: Abinaya Printer Friendly Format
It is possible that the body of a while loop will never execute. The while statement checks its condition before executing any of its statements, and if the condition evaluates false, the entire body of the while loop is skipped. The following C++ program illustrates this.
Skipping the body of the while Loop.
1: // 2: // Demonstrates skipping the body of 3: // the while loop when the condition is false. 4: 5: #include <iostream.h> 6: 7: int main() 8: { 9: int counter; 10: cout << "How many hellos?: "; 11: cin >> counter; 12: while (counter > 0) 13: { 14: cout << "Hello!\n"; 15: counter--; 16: } 17: cout << "Counter is OutPut: " << counter; 18: return 0; 19: } Output: How many hellos?: 2 Hello! Hello! Counter is OutPut: 0 How many hellos?: 0 Counter is OutPut: 0
Analysis: The user is prompted for
a starting value on line 10. This starting value is stored in the integer
variable counter. The value of counter is tested on line 12,
and decremented in the body of the while loop. The first time through counter
was set to 2, and so the body of the while loop ran twice. The
second time through, however, the user typed in 0. The value of counter
was tested on line 12 and the condition was false; counter was not
greater than 0. The entire body of the while loop was skipped,
and Hello was never printed.
What if you want to ensure that Hello is always printed at least once?
The while loop can't accomplish this, because the if condition
is tested before any printing is done. You can force the issue with an if
statement just before entering the while:
if (counter < 1) // force a minimum value counter = 1;
but that is what programmers call a "kludge," an ugly and inelegant solution.
do...while
The do...while loop executes the body of the loop before its condition is tested and ensures that the body always executes at least one time. The program below rewrites the program shown above, this time using a do...while loop.
1: // 2: // Demonstrates do while 3: 4: #include <iostream.h> 5: 6: int main() 7: { 8: int counter; 9: cout << "How many hellos? "; 10: cin >> counter; 11: do 12: { 13: cout << "Hello\n"; 14: counter--; 15: } while (counter >0 ); 16: cout << "Counter is: " << counter << endl; 17: return 0; 18: } Output: How many hellos? 2 Hello Hello Counter is: 0
Analysis: The user is prompted for
a starting value on line 9, which is stored in the integer variable counter.
In the do...while loop, the body of the loop is entered before the
condition is tested, and therefore the body of the loop is guaranteed to run at
least once. On line 13 the message is printed, on line 14 the counter is
decremented, and on line 15 the condition is tested. If the condition evaluates TRUE,
execution jumps to the top of the loop on line 13; otherwise, it falls through
to line 16.
The continue and break statements work in the do...while
loop exactly as they do in the while loop. The only difference between
a while loop and a do...while loop is when the condition
is tested.
The do...while Statement
The syntax for the do...while statement is as follows:
do statement while (condition);
statement is executed, and then condition is evaluated. If condition is TRUE, the loop is repeated; otherwise, the loop ends. The statements and conditions are otherwise identical to the while loop. Example 1
// count to 10 int x = 0; do cout << "X: " << x++; while (x < 10)
Example 2
// print lowercase alphabet. char ch = `a'; do { cout << ch << ` `; ch++; } while ( ch <= `z' );
DO use do...while when you want to ensure the loop is executed at least once. DO use while loops when you want to skip the loop if the condition is false. DO test all loops to make sure they do what you expect.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Archived Comments
1. HELP ME HOW TO ANALZE THE PROGRAM??????????
View Tutorial By: MICHELLE FLORES at 2009-01-21 23:24:52
2. .. nice one ..
.. aNg gLing nka2'amazing .
View Tutorial By: ma'kHuLet at 2010-01-19 19:24:51
3. .. nice one ..
.. aNg gLing nka2'amazing .
View Tutorial By: ma'kHuLet at 2010-01-19 19:25:08
4. .. nice one ..
.. aNg gLing nka2'amazing .
View Tutorial By: ma'kHuLet at 2010-01-19 19:25:19
5. thanks for all thanks for your help good bye
View Tutorial By: ARAS at 2010-05-14 06:50:47
6. pls help me .....its hard to understand for me th
View Tutorial By: heidi at 2010-06-27 02:31:29
7. Thank you......... It's very helpful.
View Tutorial By: Ramesh Kumar Mahto at 2010-09-01 12:12:30
8. help me pls example ng c++ program code using do
View Tutorial By: diana bernardo at 2010-10-15 23:34:19
9. amf..ang hrap hnd ko magets.....
View Tutorial By: joshua javier at 2010-10-17 05:07:10
10. Hy plz help me for also draw to flowchart.........
View Tutorial By: khadija at 2010-11-13 11:38:31
11. HI guys i am also studying c++ And now almost mast
View Tutorial By: Inayat Ullah at 2011-02-08 09:11:43
12. HI guys i am also studying c++ And now almost mast
View Tutorial By: Inayat Ullah at 2011-02-08 09:29:44
13. help me nmn qng pnu gawin s c++ ang pg ad ng 2loy2
View Tutorial By: nheb at 2011-07-22 02:58:42
14. There something at Example #2! because when i try
View Tutorial By: Paul at 2011-08-01 14:38:14
15. can you help me do this? - write a program that wi
View Tutorial By: rence at 2011-08-16 13:33:14
16. 0
View Tutorial By: zareen at 2011-08-20 08:27:28
17. i really hate do while loop statement, can u pls h
View Tutorial By: mARjO at 2011-09-01 03:50:18
18. Can you give some examples of do while & pleas
View Tutorial By: mhilan at 2011-09-01 04:54:13
19. 3 - To build features that realize the control ele
View Tutorial By: kosovari at 2012-01-30 13:52:54
20. /
//BSIT
//
#include<iost
View Tutorial By: Joshua Torres at 2012-10-05 16:59:17
21. Please, I need help, how can i write a program &qu
View Tutorial By: LULU at 2012-11-23 01:34:07
22. nice tutorial, for c++ lecture notes, and example
View Tutorial By: Jawad Khan at 2013-04-06 07:54:31