Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


1.3 Use the various loop statements that are available in C++?

Problem

I have started to sink my teeth into C++ now by writing very simple programs. I need to extend my knowledge of the language and have considered using a loop statement as I’ve seen in other languages. I need to know the various loops available in C++ and how to use them.

Technique

Looping techniques in C++ are essentially the same as they are in other languages. The loop names might be different, but for the most part, they serve the same purposes. The C++ language serves up three different types of looping statements: for, do-while, and while. The following example will show all three in action.

Steps

1.  Change to your base source directory and create a new directory named LOOP. Next, fire up your text editor.
2.  Type in the following source code, exactly as shown:
// loop.cpp - program to demonstrate C++ loops
#include <iostream>
using namespace std ;
int main()
{
   int value = 0 ;

   cout << “Beginning of while loop” << endl ;
   while( value < 5 )
   {
      cout << “The value of value is: “ << value << endl ;
      ++value ;
   }
   cout << “End of while loop” << endl ;

   cout << “\nBeginning of do-while loop” << endl ;
   do {
      cout << “The value of value is: “ << value << endl ;
   }while( value < 5 ) ;
   cout << “End of do-while loop” << endl ;
   cout << “\nBeginning of for loop” << endl ;
   for( value = 0; value < 5; value++ )
   {
      cout << “The value of value is: “ << value << endl ;
   }
   cout << “End of for loop” << endl ;
   return 0 ;
}
3.  Save the file as LOOP.CPP and exit the editor to the command line.
4.  Compile and link the LOOP.CPP source file.
5.  Run the program; the output should be as follows:
Beginning of while loop
The value of value is: 0
The value of value is: 1
The value of value is: 2
The value of value is: 3
The value of value is: 4
End of while loop

Beginning of do-while loop
The value of value is: 5
End of do-while loop

Beginning of for loop
The value of value is: 0
The value of value is: 1
The value of value is: 2
The value of value is: 3
The value of value is: 4
End of for loop

How It Works

Starting with the sixth line of code, an int has been declared and initialized with a value of 0. This integer variable will be used by all three loops. In addition to demonstrating loops, this How-To introduces you to the C++ increment (++) and less-than (<) operators.

The next statement sends a message to the screen:

cout << “Beginning of while loop” << endl ;

It is always a good idea to let the user know what is going on within a program. You can consider these messages as “progress reports.”

You begin your investigation of C++ loops with the next line of code:

while( value < 5 )

The while loop is a precondition (entryy condition) loop. First, the test expression within the parentheses is evaluated. If the result of the expression is non-zero (true), the statement(s) within the while loop’s body are executed. If the result of the expression is 0 (false), then the loop exits and control returns to the first statement after the while loop’s body. This loop can be verbally expressed as follows: “While the value stored in 1 value is less than 5, execute the statements within the loop’s body.” It is possible for a loop to never execute if the expression is initially false. For example, if the variable value is initially set to 20, the while loop will never execute.

An opening brace begins the body of a while loop and a closing brace ends the body. All statements within the body are executed as long as the loop’s expression is true. The while loop contains two statements, a message that displays the contents of the variable value and an increment (++) expression. You have seen the cout statement before; its job is to display data to the standard output. The next statement uses the increment operator ++. This operator simply increments the value of the operand by one. It is shorthand for the following statement:

value = value + 1 ;

Two versions of the increment operator exist: prefix increment and postfix increment. The version used in the while loop is the prefix increment operator. The prefix increment version increments a variable first and then evaluates the balance of the expression. The postfix increment version does the opposite: The expression is evaluated first, and then the variable is incremented. The following example shows the difference:

Line 1: if( ++check < 6 )
Line 2: if( check++ < 6 )

Assume the value of check is 5 before the if statement. In line 1, the value of check is incremented first, and then the expression is evaluated. Because check is now 6, the expression evaluates to false (check < 6) and the statements within if’s body are not executed. In line 2, the expression within the if statement (check < 6) evaluates to true, check is incremented by 1. Because the expression evaluates to true, the statements within the if’s body are executed.

After value is incremented, program control is returned to the top of the while loop. The while loop’s expression is again evaluated and if it is non-zero, the body is again executed; otherwise, the program exits the loop and control jumps to the first statement after the while loop’s body. In this example, it is a message to the user that the while loop has ended.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.