% ECE 1250 S12 Lecture M10 % Today's lecture focuses on control flow: % 1) if, else, elseif, end conditionals % 2) switch statements % 3) for loops % 4) while loops % Suppose we want to translate resistor color codes into values. % We start with colors stored in three string variables. color1 = 'brown'; color2 = 'black'; color3 = 'red'; % We now create the R_code.m function to convert colors to an R value. % R_code.m Convert three strings containing colors of bands on R to R value. %-------------------------------------------------------------------------------- % Notes on 'for' loops: A = eye(3) for index = A % index will equal the columns of eye(3) as loop executes. index = index % Print out value of index. index = 3 % We can change the value of index but that will not change the % value of index next time thru loop. A = magic(3); % Will this change the next value of index? No. The values of % the array for index is stored at the outset of the loop. end % Note: we can use a break statement to exit a loop early. % 'While' loops are convenient for certain tasks with less structure. % rand_int.m Generate random numbers until we get one that is almost an integer %-------------------------------------------------------------------------------- % Note: it's easy to end up with an infinite loop that never exits. Use CNTRL-C to stop.