1 / 7

Intro to Loops

General Knowledge Two Types of Loops. Intro to Loops. 1. 1. General Knowledge. Loops are used when blocks of code need to be repeated (without having to retype all the code!) There are 2 methods to deal with loops. 1. General Knowledge.

ewa
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. General Knowledge Two Types of Loops Intro to Loops 1

  2. 1. General Knowledge • Loops are used when blocks of code need to be repeated (without having to retype all the code!) • There are 2 methods to deal with loops

  3. 1. General Knowledge • Loops are used when blocks of code need to be repeated (without having to retype all the code!) • There are 2 methods to deal with loops • The number of time the block of code repeats is determined as the code runs • Example: Ask for username/password until the user gives a correct combination • Example: Allow the user to repeat the entire code, until s/he quits

  4. 1. General Knowledge • Loops are used when blocks of code need to be repeated (without having to retype all the code!) • There are 2 methods to deal with loops • The number of time the block of code repeats is determined as the code runs • Example: Ask for username/password until the user gives a correct combination • Example: Allow the user to repeat the entire code, until s/he quits • The programmer knows how many times the block of code needs to be repeated • Example: Ask for 5 grades, and 5 only. • Example: Suppose there are 30 items stored in a variable. Loop to display each item.

  5. General Knowledge, cont. • Regardless of which loop method is used, three (3) criteria are common to both. • When repeating anything, a loop needs: • A starting point • An ending point • A mean to know at what point the loop is at! • “A loop-control variable”

  6. 2. Two types of loops • 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 until a CONDITION is no longer met. for • A COUNTING loop. Best used when the programmer knows how many times the block of code needs to repeat.

  7. Wrap Up • Loops are meant to repeat blocks of code (WITHOUT having to copy/paste the block of code itself!) • FACT: • Sometimes, the programmer knows how many times to repeat the code-block, and sometimes, the programmer does not! • There are 2 types of loops in MATLAB • The for loop: when the number of iterations is known • The while loop: when the number of iterations is unknown • All loops require 3 criteria: a starting point, an ending point, and a variable to count from one end to the other.

More Related