<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 7 Looping - While Loops_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 7: Looping
Do, While and For Constructs
 More of this Feature
• While Loops
• Do and For Loops
• Solutions to Practice Problems
 
 Related Resources
• All lessons in the C++ Tutorial
• Learn how to compile
 

by John Kopp

Welcome to About.com's C++ tutorial. This lesson covers three constructs that are used to create loops in C++ programs. Loops can be created to execute a block of code for a fixed number of times. Alternatively, loops can be created to repetitively execute a block of code until a boolean condition changes state. For instance, the loop may continue until a condition changes from false to true, or from true to false. In this case, the block of code being executed must update the condition being tested in order for the loop to terminate at some point. If the test condition is not modified somehow within the loop, the loop will never terminate. This creates a programming bug known as an infinite loop.

While
The while loop is used to execute a block of code as long as some condition is true. If the condition is false from the start the block of code is not executed at all. Its syntax is as follows.

while (tested condition is satisfied) {
    block of code
}

Here is a simple example of the use of while. This program counts from 1 to 100.

#include <iostream>
using namespace std;

int main()
{
    int count = 1;

    while (count <= 100)
    {
        cout << count << endl;
        count += 1; //Shorthand for count = count + 1
    }

    return 0;
}

Here is a more realistic use of while. This program determines the minimum number of bits needed to store a positive integer. The largest unsigned number that can be stored in N bits is (2N - 1).

#include <iostream>
using namespace std;

int main()
{
    int bitsRequired = 1; //the power of 2
    int largest = 1; //largest number that can be stored
    int powerOf2 = 2;
    int number;

    cout << "Enter a positive integer: ";
    cin >> number;

    while (number > largest)
    {
        bitsRequired += 1; //Shorthand for bitsRequired = bitsRequired + 1
        powerOf2 = powerOf2 * 2;
        largest = powerOf2 - 1;
    }

    cout << "To store " << number << " requires ";
    cout << bitsRequired << " bits" << endl;

    return 0;
}

Next page > Do and For Loops > Page 1, 2, 3




Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.