![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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 Ive 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
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 loops 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 loops 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 loops 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 loops 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 ifs 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 ifs body are executed. After value is incremented, program control is returned to the top of the while loop. The while loops 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 loops body. In this example, it is a message to the user that the while loop has ended.
|
![]() |
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.
|