|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The next statement displays a message announcing the beginning of the do-while loop. A do-while loop is a postcondition (exit condition) loop. A do-while loop is always executed once, even if the test expression evaluates to false. This behavior is in contrast to the while loop. Note that the value contained in value is still 5, yet the cout statement is still executed. The test expression within the while portion is evaluated as false and the loop is terminated. Control resumes at the cout statement announcing the end of the do-while loop. Next, you come to the for loop. The for loop consists of three expressions. The first expression is initialization, followed by a test expression, and finally, the update (or change) expression; each expression is separated by a semicolon. The following is the programs for loop: for( value = 0; value < 5; value++ ) First, value is initialized to 0. The middle expression (value < 5) is the test expression. The test expression is evaluated; if the result is true, the loops body is executed, otherwise, the program exits the loop. After all statements in the body have executed, the third expression is evaluated. After the third expression is evaluated, control returns to the test expression. Comments Each of the C++ loops can have either a single statement or multiple statements enclosed within braces. All of the loop examples in this How-To use braces to delineate the loops body. For example, the for loop can be written this way: for( value = 0; value < 5; value++ ) cout << The value of value is: << value << endl ; You can do this because this for loop only has one statement associated with it. It is more appropriate to use braces, even if you only have one statement associated with a loop. The reason is that if you come back to add another statement to the single-statement loop, you might forget to add the braces. To demonstrate, assume you want to add a calculation that is performed for every iteration of the loop. for( value = 0; value < 5; value++ ) cout << The value of value is: << value << endl ; paycheckAmount = hours * value ; Everything appears fine; the indentation shows that the new statement is part of the loops body. Dont be fooled by appearances. By visual inspection, it appears that the program will execute both statements following the for expression. In reality, only the first statement will execute for every iteration of the loop. The second statement will only execute after the loop is finished. To correct the problem, the source code needs to look like the following: for( value = 0; value < 5; value++ ) { cout << The value of value is: << value << endl ; paycheckAmount = hours * value ; } Now, everything will work as expected. The third (update) expression in a for loop is not restricted to using the increment operator ++. You might want to use the decrement operator --. You can also increment the value by two, three, or more. The expression can also be the result of a multiplication. The following will increment value by 20 in the third expression: for( value = 1; value < 100; value = value + 20 ) { cout << The value of value is: << value << endl ; } 1.4 Create a program that uses one or more functions?Problem I am ready to move on to the more advanced features of the C++ language, specifically functions. I know that functions are used to accomplish specific tasks. I need to know how to declare and define functions. Technique Functions in C++ are the basic building blocks to modularize a program. The technique for creating functions in C++ is the same as it is in any other language. You must decide the specific functionality required for your application. If possible, you should make your functions as general as possible. This allows you to use a function in other programs. Steps
|
![]() |
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.
|