100 likes | 277 Views
Intro to Loops. General Knowledge Two Types of Loops. 1. General Knowledge. Loops are used when lines of code need to be repeatedly executed . There are 2 methods to deal with loops Make the code repeat as long as some condition is true
E N D
Intro to Loops General Knowledge Two Types of Loops
1. General Knowledge • Loops are used when lines of code need to be repeatedly executed. • There are 2 methods to deal with loops • Make the code repeat as long as some condition is true • Example1: Ask for username/password until the user gives a correct combination • Example2: Allow the user to repeat the entire code, until s/he quits • Make the code repeat a certain number of times • Example1: Ask for 5 grades, and 5 only. The number of repetitions may not be known until run-time: • Example2: User input tells the program how many grades to provide. • Example3: Loop through a vector (a certain amount of elements) ? 1 2 3
General Knowledge, cont. • Regardless of which method is used, four (4) criteria are common to both. • To repeat any process, a loop needs: • A starting point: “Initialization step” • A means to control the loop: “Loop control variable” • An “update” of the loop control variable: "Update" • A decision for continuing: “Condition”
Example of the criteria • Even on a simple repetition of counting to 10. "Control Variable" "Control Variable" "Control Variable" "Control Variable" k= k= k= k= "Initialize" "Initialize" "Initialize" "Initialize" 20 17 14 11 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 "Update" -3 "Update" +2 "Update" +1 "Update" +1 "Condition" "Condition" This is a "counting" loop. "Condition" "Condition"
Example of criteria "Control Variable" x= x = ? -9 x = ? -3 x = ? 0 x = ? 0.0 x = ? 9.5 "Initialize" "Update" a complete change using an input() command "Condition" x<=0 This is a NOT a "counting" loop.
Loop #1: while • Matlab has two loops, one for each method explained previously: while • Generic, all-purpose. Best used when the programmer does not know how many times the block of code needs to repeat. • Repeats while CONDITION is true.
Catching Errors BAD!!!!! ifdoes NOT repeat! clc clear %solving for area fprintf('\n\t*** Solving for area of circle ***\n'); %ask for radius, error when invalid radius = input('\nEnter a radius (m): '); if radius<=0 fprintf('Error. radius is invalid. Good bye!\n'); else %solve area fprintf('Area = %.2f m^2.\n',pi*radius^2); end
Catching Errors clc clear %solving for area fprintf('\n\t*** Solving for area of circle ***\n'); %ask for radius, error when invalid radius = input('\nEnter a radius (m): '); while %invalid %ask for a new radius end %solve area fprintf('Area = %.2f m^2.\n',pi*radius^2);
Loop #2: for for • A COUNTING loop. Best used when the programmer “knows” how many times the block of code needs to repeat, even if it's only decided during run-time.
Key Points • if and switchstatements allowed lines of code to be skipped but NOT repeated. However: • whileand forloops allow lines of code to be repeated • while: amount of repeats decided during run-time • for: amount of repeats determined before the loop runs (counts) • When setting up a loop (in any programming language), always have: • An initialization • One or more loop control variable(s) • An update of the control variable • A condition