01:198:107 Lecture Notes - Lecture 8: Loop Start
Document Summary
Cs 107: lecture 8: while loops, loops and nesting. Use input to get more data from the user. Use disp to print data on the screen. During evaluation, data may get sent from one function to another. Goal: add the numbers 1 through 4 for j=1:4. Problem: we have the numbers, but at different times. Solution: keep a running total in a summary variable, e. g. sum. Sum: a variable to keep the sum so far. Use the loop variable, say j, to give index of data to use in the update. E. g. count negative numbers in a vector. Number of negative numbers seen so far. Update: if current number is negative, add one to negsofar. Loop adding smaller and smaller terms to a series as long as term is not small enough. Do i need a for or a while. If you know at loop start how many repeats: for, otherwise while.