{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes-repetition1 - Repetition in C Part I Last Modified...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Repetition in C++, Part I Last Modified 7/28/08 Control Structures Motivating Example Introducing for for Loop Specifics Using for Loops ++, etc. More Examples The Four Control Structures There are four different ways control can flow through a program in any programming language: 1. Sequence: Execute every statement, in order. 2. Selection: Execute some statements (branches of the code) based on whether or not conditions are met. 3. Repetition: Execute some statement multiple times, again using some conditions to determine whether/how many times to repeat. 4. Subprogram: Leave the regular flow of a program and execute code written elsewhere, i.e. a method. Which of these have we seen so far? Where? Control Structures Motivating Example Introducing for for Loop Specifics Using for Loops ++, etc. More Examples Food for Thought Consider the following code block: cout << 1 << '\t' << pow(2,1) << endl; cout << 2 << '\t' << pow(2,2) << endl; cout << 3 << '\t' << pow(2,3) << endl; cout << 4 << '\t' << pow(2,4) << endl; cout << 5 << '\t' << pow(2,5) << endl; cout << 6 << '\t' << pow(2,6) << endl; cout << 7 << '\t' << pow(2,7) << endl; cout << 8 << '\t' << pow(2,8) << endl; cout << 9 << '\t' << pow(2,9) << endl; cout << 10 << '\t' << pow(2,10) << endl;
Image of page 1

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

View Full Document Right Arrow Icon
What does it do? What do you observe? What if we wanted to go further? Control Structures Motivating Example Introducing for for Loop Specifics Using for Loops ++, etc. More Examples Introducing the for Loop Let's take the first line of code from the block above and highlight the parts that are the same as all subsequent lines in color: cout << 1 << '\t' << pow(2, 1 ) << endl; Well, that's just about everything. In this line, all that's left is the "1" in two places. In all other lines, the number is the same. What happens between lines? Let's replace the "1" in there with a variable, i : cout << i << '\t' << pow(2, i ) << endl; Now, if we specify a few things about this i creature, we can tell the computer the same thing as the block of code above. Well, first, we need to initialize it. We'll go ahead and declare it too and set it to start at 1:
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern