1 / 10

Intro to Loops

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

spence
Download Presentation

Intro to Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Loops General Knowledge Two Types of Loops

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

  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”

  4. 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"

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

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

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

  8. 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);

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

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

More Related