Summary

 < Day Day Up > 



Program flow can be controlled using selection, iteration, and goto statements. The selection statements include the if and switch statements; the iteration statements include the while, do, and for statements.

Use the if statement to perform a simple decision operation. Be on the watch for conditions that always evaluate to false. Use the if-else statement to provide a path of execution should the condition evaluate to false. The switch statement should be used in place of nested if-else statements where the condition is an integral or enumeration type. Place a break statement at the end of every case to prevent case fall-through. Every switch should have a default case. If the default case is placed last in the switch statement a break is not necessary, but a good idea nonetheless.

Be careful not to use the assignment operator in a condition expression when you really mean to use the equal to operator. This error alone causes more heartache for novice students and professional programmers than any other I know.

The while statement places the condition test before the body statements are executed; the do places the condition test after the body statements ensuring they are executed at least once. Use a for statement when the number of loop repetitions is known. It provides a convenient format for statement variable initialization, condition testing, and expression evaluation.

If you use while, do, or for statements to perform indefinite looping either use sentinel values or provide some other way to terminate the loop.

Break and continue statements give you an extra measure of iteration statement control but use them sparingly.

Goto statements provide a way to perform unconditional jumps within your program. If you use them keep the jump within eye sight.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net