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

Info iconThis 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;
Background image of page 1

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

View Full DocumentRight 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,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

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

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

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