cpts121-6-2

# Int i j k i 2 j 3 i k 3 i i k j i k j 16 cpts 121

• Notes
• 22

This preview shows 15 out of 22 pages.

int i, j, k; i = 2; j = 3 + i++; k = 3 + ++i; i *= ++k + j--; i /= k-- + ++j;

Subscribe to view the full document.

16 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notice that we can rewrite our previous while loop with these new operators: int count; count = 0; /* Initialize counter */ while (count < 3) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); count++; /* increment counter */ }
17 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notice that we can also rewrite our previous for loop with these new operators: int count; for (count = 0; count < 3; count++ ) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); }

Subscribe to view the full document.

18 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Accumulating a value Suppose that we want to keep track of the total number of tolls collected by the tollbooth. We could accumulate this value from within the loop body: int count, toll_total; for (count = 0, toll_total = 0 ; count < 3; count++) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); toll_total += toll; /* add toll to toll_total */ }
19 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Loop increments other than 1

Subscribe to view the full document.

20 CptS 121 - 9/30/09 Prof. Chris Hundhausen You Try It With a partner, complete the following function: /* fib -- returns the nth number in the Fibonacci sequence. Note that the Fibonacci sequence is defined as follows: Fib(0) = 0; fib(1) = 1; fib(n) = fib(n-1) + fib(n-2) (for n>1) */ int fib(int n) { /* Fill this in */ }
21 CptS 121 - 9/30/09 Prof. Chris Hundhausen You Try It (cont.) Solution int fib(int n) { /* one student group fills in their solution here. */ }

Subscribe to view the full document.

22 CptS 121 - 9/30/09 Prof. Chris Hundhausen Next Lecture… We'll discuss several additional loop patterns: Conditional loops Sentinel-controlled loops Endfile-controlled loops Flag-controlled loops
You've reached the end of this preview.
• Spring '09
• hundhaus
• Prof. Chris Hundhausen

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern