110 likes | 126 Views
Learn about the three common types of loops in computer programming, including Infinite Loops, Counted Loops, and Conditional Loops. Explore the components and properties of loops, such as loop variables and conditions. Delve into examples of Counted Loops and Conditional Loops in NetLogo.
E N D
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski
Types of Loops • Already know that there are three common types of loops in most programming languages: • Infinite Loops • Counted Loops • Conditional Loops
Components of a Loop • Loops have some properties in common: • Body of the loop: The portion of the code that is repeated. • Iteration:Number of times the loop is executed • Some types of loops have other properties • Loop Variable: A variable that is incremented during the looping process • Loop Condition: A conditional (true of false) statement that is checked during looping
Infinite Loops • An infinite loop repeats the body of the loop until one of the following occurs: • A“stop” command in the program • Manually turned off. • A runtime error. • Already familiar with this in NetLogo
Counted Loops • Repeated a fixed maximum number of times. • Must know how many times • Before programming • Be able to calculate maximum number of times
Counted Loops in Netlogo • Netlogo has a specific command for counted loops, the REPEAT command repeat Nnumber [ commands ] • The commands in the square brackets [] are repeated Nnumber of times.
Conditional Loops • Repeated while a certain conditional statement is true. • Must establish the condition statement initially and it must be true for the loop to start • The condition must become false during the looping process in order for the loop to stop, otherwise the loop becomes infinite.
Conditional Loops in Netlogo • Netlogo has a specific command for conditional loops, the WHILE command while [condition] [ commands ] • The commands in the square brackets [] are repeated while the condition is true. • When the condition becomes false, the loop is exited.
Summary • Three types of loops • Infinite Loops • Counted Loops- the REPEAT command repeat Nnumber [ commands ] • Conditional Loops - the WHILE command while [condition] [ commands ]