Chp7 - C+ PROGRAMMING Chapter 7 More Program Flow Looping...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ PROGRAMMING Chapter 7 More Program Flow Looping The Roots of Looping goto Listing 7.1. Looping with the keyword goto . Why goto Is Shunned The goto Statement , while Loops Listing 7.2. while loops . The while Statement More Complicated while Statements Listing 7.3. Complex while loops . continue and break Listing 7.4. break and continue . The continue Statement , The break Statement while (1) Loops Listing 7.5. while (1) loops . do. ..while Loops Listing 7.6. Skipping the body of the while Loop . do. ..while Listing 7.7. Demonstrates do. ..while loop . The do. ..while Statement for Loops Listing 7.8. While reexamined . Listing 7.9. Demonstrating the for loop . The for Statement Advanced for Loops Listing 7.10. Demonstrating multiple statements in for loops . Listing 7.11. Null statements in for loops . Listing 7.12. Illustrating empty for loop statement . Empty for Loops Listing 7.13. Illustrates the null statement in a for loop . Nested Loops Listing 7.14. Illustrates nested for loops . Scoping in for Loops Summing Up Loops Listing 7.15. Solving the nth Fibonacci number using iteration. , switch Statements Listing 7.16. Demonstrating the switch statement . The switch Statement Using a switch Statement with a Menu Listing 7.17. Demonstrating a forever loop . Summary , , Quiz , Exercises
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 7 More Program Flow Programs accomplish most of their work by branching and looping. On Chapter 4, "Expressions and Statements," you learned how to branch your program using the if statement. ToChapter you learn What loops are and how they are used. How to build various loops. An alternative to deeply-nested if/else statements. Looping Many programming problems are solved by repeatedly acting on the same data. There are two ways to do this: recursion (discussed yesterChapter) and iteration. Iteration means doing the same thing again and again. The principal method of iteration is the loop. The Roots of Looping goto In the primitive Chapters of early computer science, programs were nasty, brutish, and short. Loops consisted of a label, some statements, and a jump. In C++, a label is just a name followed by a colon ( : ). The label is placed to the left of a legal C++ statement, and a jump is accomplished by writing goto followed by the label name. Listing 7.1 illustrates this. Listing 7.1. Looping with the keyword goto. 1: // Listing 7.1 2: // Looping with goto 3: #include <iostream.h> 4: int main() { 5: int counter = 0; // initialize counter 6: loop: counter ++; // top of the loop 7: cout << "counter: " << counter << "\n"; 8: if (counter < 5) // test the value 9: goto loop; // jump to the top 10: 11: cout << "Complete. Counter: " << counter << ".\n"; 12: return 0; 13: } Output: counter: 1 counter: 2 counter: 3 counter: 4 counter: 5 Complete. Counter: 5.
Background image of page 2
Analysis: On line 8, counter
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CS 131 taught by Professor Clayton during the Spring '08 term at Bethune Cookman.

Page1 / 30

Chp7 - C+ PROGRAMMING Chapter 7 More Program Flow Looping...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online